Knowledge Card - String Methods

String Methods

  1. len(): Returns the length of a string.

    • Example: len('hello') returns 5.
  2. upper(): Converts all characters to uppercase.

    • Example: 'hello'.upper() returns 'HELLO'.
  3. lower(): Converts all characters to lowercase.

    • Example: 'HELLO'.lower() returns 'hello'.
  4. replace(): Replaces occurrences of a substring with another.

    • Example: 'hello world'.replace('world', 'there') returns 'hello there'.
  5. split(): Splits a string into a list based on a delimiter.

    • Example: 'apple,banana,cherry'.split(',') returns ['apple', 'banana', 'cherry'].