pyguides

Reference

String Methods

Methods available on Python str objects.

  1. str.capitalize()

    The string capitalize method returns a copy of the string with its first character capitalized and the rest converted to lowercase

  2. str.casefold()

    The casefold method returns a casefolded copy of a string, useful for case-insensitive string matching and Unicode-aware lowercase conversion

  3. str.center()

    The string center method returns a centered string of the specified width, padded with a specified fill character

  4. str.count()

    The string count() method returns the number of non-overlapping occurrences of a substring in the string

  5. str.encode()

    The string encode method returns an encoded version of the string as a bytes object, with support for various character encodings

  6. str.endswith()

    The string endswith method returns True if the string ends with the specified suffix, otherwise False

  7. str.expandtabs()

    The expandtabs method returns a copy of the string with tab characters expanded to spaces

  8. str.find()

    Find the index of a substring in a string. Returns -1 if not found.

  9. str.format_map()

    The string format_map method performs string formatting using a mapping object directly without copying it

  10. str.format()

    Format values into strings using replacement fields and format specifiers

  11. str.isalnum()

    The isalnum method returns True if all characters in the string are alphanumeric (letters or digits) and the string is not empty

  12. str.isalpha()

    The isalpha method returns True if all characters in the string are alphabetic letters and the string is not empty

  13. str.isascii()

    The string isascii method returns True if all characters in the string are ASCII, False otherwise

  14. str.isdecimal()

    Check if all characters in a string are decimal characters (under 160 chars)

  15. str.isdigit()

    Check if all characters in a string are digits.

  16. str.isidentifier()

    Check if a string is a valid Python identifier

  17. str.islower()

    The islower method returns True if all cased characters in a string are lowercase and the string contains at least one cased character

  18. str.isnumeric()

    Check if all characters in a string are numeric characters (supports Unicode)

  19. str.isprintable()

    The isprintable method returns True if all characters in the string are printable

  20. str.isspace()

    Returns True if all characters in the string are whitespace characters and the string is not empty.

  21. str.istitle()

    The istitle method returns True if the string is titlecased where each word starts with an uppercase character followed by lowercase letters

  22. str.isupper()

    Returns True if all cased characters in a string are uppercase and there is at least one cased character.

  23. str.join()

    The string join method returns a string by joining iterable elements with a separator

  24. str.ljust()

    The string ljust method returns a left-justified version of the string padded with spaces or a custom character

  25. str.lower()

    The lower method returns a copy of the string with all characters converted to lowercase

  26. str.lstrip()

    Remove leading characters from a string. Defaults to whitespace.

  27. str.maketrans()

    The str.maketrans() method creates a translation table for use with the translate() method

  28. str.partition()

    Split a string into three parts using a separator, returning a 3-tuple.

  29. str.removeprefix()

    Remove a prefix from a string if present. Returns the original string unchanged if the prefix doesn't match.

  30. str.removesuffix()

    Remove a suffix from a string if present. Returns the original string unchanged if the suffix doesn't match.

  31. str.replace()

    Replace substrings in a string with a new value. Supports optional count limit.

  32. str.rfind()

    Find the highest index of a substring in a string. Returns -1 if not found.

  33. str.rjust()

    Right-justify a string in a field of given width.

  34. str.rpartition()

    Split a string from the right into three parts using a separator, returning a 3-tuple.

  35. str.rsplit()

    Split a string from the right into a list of substrings with rsplit().

  36. str.rstrip()

    Remove trailing characters from a string. Defaults to whitespace.

  37. str.split()

    Split a string into a list of substrings with the split() method.

  38. str.splitlines()

    Split a string into a list of lines, handling various line ending styles.

  39. str.startswith()

    Check if a string starts with a given prefix

  40. str.strip()

    Remove leading and trailing characters from a string. Defaults to whitespace.

  41. str.swapcase()

    Returns a string with uppercase characters converted to lowercase and vice versa.

  42. str.title()

    Returns a titlecased version of the string where words start with uppercase characters.

  43. str.translate()

    The translate method returns a copy of the string where each character has been mapped through a translation table

  44. str.upper()

    Converts all characters in a string to uppercase, returning a new string.

  45. str.zfill()

    Pad a string with zeros on the left to reach a desired width.