SH

Week-7-1

Introduction to Strings

  • Strings: Sequence of characters identified by specific index starting from zero.

String Slicing

  • Definition: Substring extraction using slice notation string[start:end] with start inclusive and end exclusive.

  • Examples:

    • my_string[0:2] -> "He"

    • my_string[7:10] -> "Wor"

  • New String Objects: Slicing creates new strings (e.g., animal = my_string[4:7]).

Using Slice Notation

  • Missing End Index: my_string[8:] extracts till the end.

  • Negative Indices: Count from the end of the string (e.g., my_string[-5]).

Understanding Stride

  • Definition: string[start:end:stride] allows index increments (e.g., my_string[0:9:2]).

String Methods

  • Replace: string.replace(old, new, count) for substring replacement.

  • Find Methods: find(substring) locates first occurrence; count(substring) counts occurrences.

  • Membership: Use in to check for substring presence.

String Comparisons

  • Lexicographical Order: Based on ASCII values (e.g., "y" > "a").

String Character Checks and Formatting

  • Methods: isalpha(), isdigit(), islower(), isupper(), isspace() for various checks.

  • Formatting: strip() for whitespace, capitalize() for first letter, and title() to capitalize each word.