🔤

Mastering String Methods in Python

Jun 4, 2025

100 Days Of Code - String Methods Lecture

Overview

  • The lecture focuses on various methods for handling strings in Python.
  • Strings are an essential data type, and the session covers operations that can be performed on strings.

Key String Methods

Immutability of Strings

  • Strings in Python are immutable.
  • You cannot change a string in place; instead, operations return a new string.
  • Example: a = "Harry"; a.upper() returns a new string "HARRY" without changing a.

Upper and Lower Methods

  • upper(): Converts all characters in a string to uppercase.
  • lower(): Converts all characters in a string to lowercase.

Strip Methods

  • rstrip(): Removes trailing characters from a string.
    • Example: a.rstrip("!") removes exclamation marks from the end.

Replace Method

  • replace(old, new): Replaces all occurrences of a substring with another substring.
    • Example: a.replace("harry", "John") replaces "harry" with "John".

Split Method

  • split(): Splits a string into a list based on a delimiter.
    • Example: Splitting a string on space creates a list of words.

Capitalize Method

  • capitalize(): Capitalizes the first character of the string and lowers all others.

Center Method

  • center(width): Centers the string in a field of given width by padding with spaces.

Count Method

  • count(substring): Returns the number of occurrences of a substring in the string.

Endswith Method

  • endswith(suffix): Checks if the string ends with the specified suffix.
    • Can specify start and end positions.

Find and Index Methods

  • find(substring): Returns the index of the first occurrence of the substring. Returns -1 if not found.
  • index(substring): Similar to find, but raises an exception if the substring is not found.

Alphanumeric Check Methods

  • isalnum(): Returns True if all characters in the string are alphanumeric.
  • isalpha(): Returns True if all characters in the string are alphabetic.

Case Check Methods

  • islower(): Checks if all alphabetic characters in the string are lowercase.
  • isupper(): Checks if all alphabetic characters in the string are uppercase.

Printable Check Method

  • isprintable(): Checks if all characters in the string are printable.

Space Check Method

  • isspace(): Returns True if there are only whitespace characters in the string.

Title Check Method

  • istitle(): Returns True if the string is titlecased (first letter of each word is uppercase).

Swapcase and Title Methods

  • swapcase(): Swaps case of all letters in the string.
  • title(): Converts the string to title case.

Additional Information

  • The lecture was presented with examples using Python code in a Repl environment.
  • There was a brief discussion on the setup used for recording the lecture, including the use of Replit and video tools.

Conclusion

  • Understanding string methods is crucial for manipulating text data in Python.
  • Practicing with the provided examples can enhance familiarity with these methods.