Python String functions


Here is a list of commonly used string functions in Python:

  1. len(str) – Returns the length of the string.
  2. str.upper() – Converts all characters to uppercase.
  3. str.lower() – Converts all characters to lowercase.
  4. str.capitalize() – Capitalizes the first character of the string.
  5. str.title() – Converts the first character of each word to uppercase.
  6. str.strip() – Removes leading and trailing whitespace.
  7. str.lstrip() – Removes leading whitespace (from the left).
  8. str.rstrip() – Removes trailing whitespace (from the right).
  9. str.replace(old, new) – Replaces all occurrences of a substring with another substring.
  10. str.find(sub) – Returns the lowest index of the substring, or -1 if not found.
  11. str.index(sub) – Returns the lowest index of the substring, raises an error if not found.
  12. str.count(sub) – Returns the number of occurrences of a substring.
  13. str.startswith(prefix) – Returns True if the string starts with the given prefix.
  14. str.endswith(suffix) – Returns True if the string ends with the given suffix.
  15. str.split(separator) – Splits the string into a list based on the separator.
  16. str.join(iterable) – Joins elements of an iterable (like a list) into a string with a separator.
  17. str.isalpha() – Returns True if all characters in the string are alphabetic.
  18. str.isdigit() – Returns True if all characters in the string are digits.
  19. str.isalnum() – Returns True if all characters in the string are alphanumeric.
  20. str.isspace() – Returns True if the string contains only whitespace.
  21. str.swapcase() – Converts uppercase to lowercase and vice versa.
  22. str.zfill(width) – Pads the string on the left with zeros to fill the specified width.
  23. str.center(width) – Centers the string with spaces to fill the specified width.
  24. str.ljust(width) – Left-aligns the string with spaces to fill the specified width.
  25. str.rjust(width) – Right-aligns the string with spaces to fill the specified width.
  26. str.partition(sep) – Splits the string into three parts: before, separator, and after.
  27. str.rpartition(sep) – Splits the string into three parts: before, separator, and after, starting from the right.
  28. str.splitlines() – Splits the string at line breaks and returns a list of lines.
  29. str.encode(encoding) – Encodes the string into bytes using the specified encoding.
  30. str.decode(encoding) – Decodes bytes back into a string using the specified encoding.
  31. str.casefold() – Returns a case-insensitive version of the string (similar to lower() but more aggressive).
  32. str.format(*args, **kwargs) – Formats the string by inserting the values of the arguments.
  33. str.format_map(mapping) – Similar to format(), but takes a single mapping argument (like a dictionary).
  34. str.expandtabs(tabsize) – Replaces tabs in the string with spaces; by default, tabsize is 8.
  35. str.translate(table) – Returns a string where each character is mapped based on the provided translation table.
  36. str.maketrans(x, y) – Creates a translation table for translate().

These string functions allow you to perform various operations on strings such as formatting, splitting, replacing, and checking properties.