Python String Methods Lecture

Jul 18, 2024

Python String Methods

Introduction

  • 47 string methods in Python.
  • Excluding dunder methods (e.g., __len__)

Common Methods

  1. capitalize()
    • Capitalizes the first character.
  2. casefold()
    • For caseless comparisons.

Less Common Methods

  1. center(width, [fillchar])
    • Centers the string with optional fillchar.
  2. count(substring)
    • Counts occurrences of substring.
  3. encode([encoding[, errors]])
    • Encodes the string.
  4. endswith(suffix[, start[, end]])
    • Checks if the string ends with suffix.
  5. expandtabs([tabsize])
    • Expands tabs to spaces.
  6. find(sub[, start[, end]])
    • Finds the first occurrence of sub.
  7. **format(*args, kwargs)
    • Formats the string.
  8. format_map(mapping)
    • Uses dictionary for formatting.

Methods for Character Type Checking

  1. isalnum()
    • Checks if all characters are alphanumeric.
  2. isalpha()
    • Checks if all characters are alphabetic.
  3. isascii()
    • Checks if all characters are ASCII.
  4. isdecimal()
    • Checks if all characters are decimals.
  5. isdigit()
    • Checks if all characters are digits.
  6. isnumeric()
    • Checks if all characters are numeric.
  7. isidentifier()
    • Checks for a valid identifier.
  8. islower()
    • Checks if all characters are lowercase.
  9. isprintable()
    • Checks if all characters are printable.
  10. isspace()
    • Checks if all characters are whitespace.
  11. istitle()
    • Checks if the string is a title.
  12. isupper()
    • Checks if all characters are uppercase.

String Manipulation Methods

  1. join(iterable)
    • Joins elements of an iterable with the string acting as a separator.
  2. ljust(width[, fillchar])
    • Left-justifies the string.
  3. lower()
    • Converts all characters to lowercase.
  4. lstrip([chars])
    • Strips leading characters.
  5. maketrans(x[, y[, z]])
    • Returns a translation table.
  6. translate(table)
    • Translates string using a translation table.
  7. partition(sep)
    • Splits the string into a tuple (head, sep, tail).
  8. removeprefix(prefix)
    • Removes the specified prefix.
  9. removesuffix(suffix)
    • Removes the specified suffix.
  10. replace(old, new[, count])
    • Replaces occurrences of old with new.
  11. rfind(sub[, start[, end]])
    • Finds the last occurrence of sub.
  12. rindex(sub[, start[, end]])
    • Like rfind but raises ValueError if sub is not found.
  13. rjust(width[, fillchar])
    • Right-justifies the string.
  14. rpartition(sep)
    • Splits into a tuple (tail, sep, head) from the right.
  15. rsplit([sep[, maxsplit]])
    • Splits the string from the right.
  16. rstrip([chars])
    • Strips trailing characters.
  17. split([sep[, maxsplit]])
    • Splits the string from the left.
  18. splitlines([keepends])
    • Splits on line breaks.
  19. startswith(prefix[, start[, end]])
    • Checks if string starts with the prefix.
  20. strip([chars])
    • Strips leading and trailing characters.
  21. swapcase()
    • Swaps case; converts uppercase to lowercase and vice versa.
  22. title()
    • Converts string to title case.
  23. upper()
    • Converts all characters to uppercase.
  24. zfill(width)
    • Pads string on the left with zeros.

Conclusion

  • Important to know various string methods for efficient string manipulation in Python.
  • Practice with these methods to understand their use-cases.
  • Reach out with questions or suggestions.
  • Happy New Year!