String methods reference¶
This page provides a comprehensive reference for all methods available on Python str objects. Each method is grouped by category, with a summary table followed by detailed signatures and descriptions.
For the official documentation, see the Python str type documentation.
All string methods return new values. Strings in Python are immutable, so no method modifies the original string in place.
Case conversion¶
Methods that return a copy of the string with the case of characters changed.
| Method | Description | Example | Result |
|---|---|---|---|
str.upper() |
Convert all characters to uppercase | "hello".upper() |
"HELLO" |
str.lower() |
Convert all characters to lowercase | "HELLO".lower() |
"hello" |
str.capitalize() |
Capitalise the first character, lowercase the rest | "hello WORLD".capitalize() |
"Hello world" |
str.title() |
Capitalise the first character of each word | "hello world".title() |
"Hello World" |
str.swapcase() |
Swap uppercase to lowercase and vice versa | "Hello".swapcase() |
"hELLO" |
str.casefold() |
Aggressive lowercase for caseless matching | "Straße".casefold() |
"strasse" |
Detailed signatures¶
str.upper()¶
Returns a copy of the string with all cased characters converted to uppercase. The method handles Unicode characters, not only ASCII.
str.lower()¶
Returns a copy of the string with all cased characters converted to lowercase.
str.capitalize()¶
Returns a copy of the string with the first character capitalised and the remainder lowercased.
str.title()¶
Returns a titlecased version of the string, where words start with an uppercase character and the remaining characters are lowercase. The algorithm uses a simple word boundary definition: any sequence of characters that are not letters is treated as a word separator. This can produce unexpected results with apostrophes and other punctuation.
str.swapcase()¶
Returns a copy of the string with uppercase characters converted to lowercase and vice versa. Note that s.swapcase().swapcase() is not necessarily equal to s for certain Unicode characters.
str.casefold()¶
Returns a casefolded copy of the string. Casefolding is similar to lowercasing but more aggressive, as it is intended to remove all case distinctions in a string. This is useful for caseless matching. For example, the German lowercase letter ß is equivalent to ss; since it is already lowercase, lower() would do nothing, but casefold() converts it to ss.
Search and test¶
Methods for finding substrings and testing for the presence of content within a string.
| Method | Description | Example | Result |
|---|---|---|---|
str.find() |
Find the lowest index of a substring, or -1 |
"hello".find("ll") |
2 |
str.rfind() |
Find the highest index of a substring, or -1 |
"hello hello".rfind("hello") |
6 |
str.index() |
Like find(), but raises ValueError if not found |
"hello".index("ll") |
2 |
str.rindex() |
Like rfind(), but raises ValueError if not found |
"hello hello".rindex("hello") |
6 |
str.count() |
Count non-overlapping occurrences of a substring | "banana".count("an") |
2 |
str.startswith() |
Test whether the string starts with a prefix | "hello".startswith("he") |
True |
str.endswith() |
Test whether the string ends with a suffix | "hello".endswith("lo") |
True |
in operator |
Test whether a substring exists within the string | "ll" in "hello" |
True |
Detailed signatures¶
str.find(sub[, start[, end]])¶
Returns the lowest index in the string where substring sub is found within the slice s[start:end]. Returns -1 if sub is not found.
>>> "hello world".find("world")
6
>>> "hello world".find("python")
-1
>>> "hello world".find("o", 5)
7
str.rfind(sub[, start[, end]])¶
Returns the highest index in the string where substring sub is found within the slice s[start:end]. Returns -1 if sub is not found.
str.index(sub[, start[, end]])¶
Like find(), but raises a ValueError when the substring is not found.
str.rindex(sub[, start[, end]])¶
Like rfind(), but raises a ValueError when the substring is not found.
str.count(sub[, start[, end]])¶
Returns the number of non-overlapping occurrences of substring sub in the range [start, end].
str.startswith(prefix[, start[, end]])¶
Returns True if the string starts with the specified prefix. The prefix can also be a tuple of prefixes to test for.
>>> "hello".startswith("he")
True
>>> "hello".startswith(("he", "wo"))
True
>>> "hello".startswith("el", 1)
True
str.endswith(suffix[, start[, end]])¶
Returns True if the string ends with the specified suffix. The suffix can also be a tuple of suffixes to test for.
The in operator¶
The in operator is not a method but a membership test. It returns True if sub is found anywhere within s.
String testing¶
Methods that test the content of a string and return a boolean value. All of these methods return False for an empty string.
| Method | Description | Example | Result |
|---|---|---|---|
str.isalpha() |
All characters are alphabetic | "hello".isalpha() |
True |
str.isdigit() |
All characters are digits | "123".isdigit() |
True |
str.isalnum() |
All characters are alphanumeric | "abc123".isalnum() |
True |
str.isspace() |
All characters are whitespace | " \t\n".isspace() |
True |
str.isupper() |
All cased characters are uppercase | "HELLO".isupper() |
True |
str.islower() |
All cased characters are lowercase | "hello".islower() |
True |
str.istitle() |
String is in titlecase | "Hello World".istitle() |
True |
str.isnumeric() |
All characters are numeric | "½".isnumeric() |
True |
str.isdecimal() |
All characters are decimal | "123".isdecimal() |
True |
str.isidentifier() |
String is a valid Python identifier | "my_var".isidentifier() |
True |
str.isprintable() |
All characters are printable | "hello".isprintable() |
True |
str.isascii() |
All characters are ASCII | "hello".isascii() |
True |
Detailed signatures¶
str.isalpha()¶
Returns True if all characters in the string are alphabetic and there is at least one character. Alphabetic characters are those defined in the Unicode character database as "Letter".
str.isdigit()¶
Returns True if all characters in the string are digits and there is at least one character. Digits include decimal characters and digits that need special handling, such as superscript digits.
str.isalnum()¶
Returns True if all characters in the string are alphanumeric (alphabetic or digit) and there is at least one character.
str.isspace()¶
Returns True if all characters in the string are whitespace characters and there is at least one character. Whitespace characters include space, tab, newline, carriage return, form feed, and vertical tab, among other Unicode whitespace characters.
str.isupper()¶
Returns True if all cased characters in the string are uppercase and there is at least one cased character. Uncased characters (such as digits and punctuation) are ignored.
str.islower()¶
Returns True if all cased characters in the string are lowercase and there is at least one cased character.
str.istitle()¶
Returns True if the string is titlecased: uppercase characters may only follow uncased characters, and lowercase characters may only follow cased characters.
str.isnumeric()¶
Returns True if all characters in the string are numeric characters and there is at least one character. Numeric characters include digit characters and all characters that have the Unicode numeric value property, such as fractions and Roman numerals.
str.isdecimal()¶
Returns True if all characters in the string are decimal characters and there is at least one character. Decimal characters are those that can be used to form numbers in base 10. This is a stricter test than isdigit().
str.isidentifier()¶
Returns True if the string is a valid identifier according to the Python language definition. Note that this does not check whether the string is a reserved keyword. Use keyword.iskeyword() to test for reserved identifiers.
str.isprintable()¶
Returns True if all characters in the string are printable or the string is empty. Non-printable characters are those defined in the Unicode character database as "Other" or "Separator", with the exception of the ASCII space (0x20), which is considered printable.
str.isascii()¶
Returns True if the string is empty or all characters in the string are ASCII (code points in the range U+0000 to U+007F).
Transformation¶
Methods that return transformed copies of the string.
| Method | Description | Example | Result |
|---|---|---|---|
str.strip() |
Remove leading and trailing characters | " hello ".strip() |
"hello" |
str.lstrip() |
Remove leading characters | " hello ".lstrip() |
"hello " |
str.rstrip() |
Remove trailing characters | " hello ".rstrip() |
" hello" |
str.replace() |
Replace occurrences of a substring | "hello".replace("l", "r") |
"herro" |
str.translate() |
Translate characters using a mapping table | "abc".translate({97: "A"}) |
"Abc" |
str.maketrans() |
Create a translation table | str.maketrans("abc", "ABC") |
(mapping) |
str.expandtabs() |
Replace tab characters with spaces | "a\tb".expandtabs(4) |
"a b" |
str.removeprefix() |
Remove a prefix if present | "TestCase".removeprefix("Test") |
"Case" |
str.removesuffix() |
Remove a suffix if present | "MixIn".removesuffix("In") |
"Mix" |
Detailed signatures¶
str.strip([chars])¶
Returns a copy of the string with leading and trailing characters removed. The chars argument is a string specifying the set of characters to remove. If omitted or None, whitespace characters are removed. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped.
str.lstrip([chars])¶
Returns a copy of the string with leading characters removed. Behaves identically to strip(), but only removes characters from the left side.
str.rstrip([chars])¶
Returns a copy of the string with trailing characters removed. Behaves identically to strip(), but only removes characters from the right side.
str.replace(old, new[, count])¶
Returns a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
str.translate(table)¶
Returns a copy of the string in which each character has been mapped through the given translation table. The table must be a mapping of Unicode ordinals to Unicode ordinals, strings, or None. Characters mapped to None are deleted.
str.maketrans(x[, y[, z]])¶
# Single argument form
str.maketrans(mapping: dict[int | str, str | int | None]) -> dict[int, str | int | None]
# Two argument form
str.maketrans(from: str, to: str) -> dict[int, int]
# Three argument form
str.maketrans(from: str, to: str, delete: str) -> dict[int, int | None]
This is a static method that returns a translation table suitable for use with str.translate().
With one argument, mapping must be a dictionary mapping Unicode ordinals or single characters to Unicode ordinals, strings, or None.
With two arguments, from and to must be strings of equal length, and each character in from will be mapped to the character at the same position in to.
With three arguments, each character in delete will be mapped to None.
str.expandtabs(tabsize=8)¶
Returns a copy of the string where all tab characters are expanded using spaces. The column number is reset to zero at each newline. Tab positions occur every tabsize characters.
str.removeprefix(prefix)¶
If the string starts with the prefix string, returns string[len(prefix):]. Otherwise, returns a copy of the original string. Available since Python 3.9.
str.removesuffix(suffix)¶
If the string ends with the suffix string and that suffix is not empty, returns string[:-len(suffix)]. Otherwise, returns a copy of the original string. Available since Python 3.9.
Split and join¶
Methods for splitting strings into lists and joining lists into strings.
| Method | Description | Example | Result |
|---|---|---|---|
str.split() |
Split by separator (from left) | "a,b,c".split(",") |
["a", "b", "c"] |
str.rsplit() |
Split by separator (from right) | "a,b,c".rsplit(",", 1) |
["a,b", "c"] |
str.splitlines() |
Split by line boundaries | "a\nb\n".splitlines() |
["a", "b"] |
str.partition() |
Split into three parts at first separator | "a:b:c".partition(":") |
("a", ":", "b:c") |
str.rpartition() |
Split into three parts at last separator | "a:b:c".rpartition(":") |
("a:b", ":", "c") |
str.join() |
Join an iterable with the string as separator | ",".join(["a", "b"]) |
"a,b" |
Detailed signatures¶
str.split(sep=None, maxsplit=-1)¶
Returns a list of the words in the string, using sep as the delimiter. If maxsplit is given, at most maxsplit splits are done (resulting in at most maxsplit + 1 elements).
When sep is None or not specified, any whitespace string is a separator, and empty strings are removed from the result. Consecutive whitespace is treated as a single separator. When sep is specified, consecutive delimiters are not grouped together and produce empty strings.
>>> "hello world".split()
['hello', 'world']
>>> " hello world ".split()
['hello', 'world']
>>> "a,,b,,c".split(",")
['a', '', 'b', '', 'c']
>>> "a,b,c,d".split(",", 2)
['a', 'b', 'c,d']
str.rsplit(sep=None, maxsplit=-1)¶
Behaves identically to split(), except that splits are performed from the right. This only makes a difference when maxsplit is specified.
str.splitlines(keepends=False)¶
Returns a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is True. This method recognises a variety of line boundary representations, including \n, \r\n, \r, \v, \f, \x1c, \x1d, \x1e, \x85, \u2028, and \u2029.
>>> "hello\nworld\n".splitlines()
['hello', 'world']
>>> "hello\nworld\n".splitlines(True)
['hello\n', 'world\n']
>>> "hello\r\nworld".splitlines()
['hello', 'world']
str.partition(sep)¶
Splits the string at the first occurrence of sep and returns a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, returns a 3-tuple containing the original string and two empty strings.
>>> "user@example.com".partition("@")
('user', '@', 'example.com')
>>> "hello".partition("@")
('hello', '', '')
str.rpartition(sep)¶
Splits the string at the last occurrence of sep and returns a 3-tuple. If the separator is not found, returns a 3-tuple containing two empty strings followed by the original string.
>>> "path/to/file.txt".rpartition("/")
('path/to', '/', 'file.txt')
>>> "hello".rpartition("/")
('', '', 'hello')
str.join(iterable)¶
Returns a string which is the concatenation of the strings in iterable. The string on which join() is called serves as the separator between elements. A TypeError is raised if any value in the iterable is not a string.
>>> ",".join(["apple", "banana", "cherry"])
'apple,banana,cherry'
>>> " ".join(["hello", "world"])
'hello world'
>>> "\n".join(["line 1", "line 2", "line 3"])
'line 1\nline 2\nline 3'
Alignment and padding¶
Methods for aligning and padding strings to a specified width.
| Method | Description | Example | Result |
|---|---|---|---|
str.center() |
Centre the string within a given width | "hi".center(10) |
" hi " |
str.ljust() |
Left-justify the string within a given width | "hi".ljust(10) |
"hi " |
str.rjust() |
Right-justify the string within a given width | "hi".rjust(10) |
" hi" |
str.zfill() |
Pad with zeros on the left to fill a given width | "42".zfill(5) |
"00042" |
Detailed signatures¶
str.center(width[, fillchar])¶
Returns the string centred within a string of length width. Padding is done using the specified fill character (default is an ASCII space). The original string is returned if width is less than or equal to len(s).
str.ljust(width[, fillchar])¶
Returns the string left-justified within a string of length width. Padding is done using the specified fill character.
str.rjust(width[, fillchar])¶
Returns the string right-justified within a string of length width. Padding is done using the specified fill character.
str.zfill(width)¶
Returns a copy of the string left-filled with ASCII 0 digits to make a string of length width. A leading sign prefix (+ or -) is handled by inserting the padding after the sign character rather than before. The original string is returned if width is less than or equal to len(s).
Encoding¶
Methods related to encoding strings as bytes.
| Method | Description | Example | Result |
|---|---|---|---|
str.encode() |
Encode the string to bytes | "hello".encode("utf-8") |
b"hello" |
Detailed signatures¶
str.encode(encoding="utf-8", errors="strict")¶
Returns the string encoded as a bytes object using the specified encoding. The errors argument specifies the error handling scheme. The default value of "strict" causes a UnicodeEncodeError to be raised on encoding failures. Other permitted values include "ignore", "replace", "xmlcharrefreplace", "backslashreplace", and any other name registered via codecs.register_error().
>>> "hello".encode()
b'hello'
>>> "café".encode("utf-8")
b'caf\xc3\xa9'
>>> "café".encode("ascii", errors="replace")
b'caf?'
>>> "café".encode("ascii", errors="ignore")
b'caf'
See also¶
- Python
strtype documentation -- official reference for all string methods - String formatting reference -- f-strings,
str.format(), and the format specification mini-language - String constants reference -- constants from the
stringmodule - Unicode HOWTO -- working with Unicode in Python
- Text Sequence Type -- the
strtype itself, including indexing, slicing, and common operations