Python String Methods

About This Index
This page is the Python String Methods index for the tutorial track. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example uses fixed text, so the output you see is the output you get.
Learn what a string is, quotes, f-strings, indexing, and immutability on the Strings page. Use this page when you need a specific method such as strip(), split(), or replace().
Every method below belongs to the built-in str type, so no import is needed. A string can never be changed after it is created, so no method edits the string you call it on. Each one returns something new: another string, a list, a number, a bool, or a tuple. The Returns column tells you which, and you must assign the result to keep it.
How to Call a String Method
Write the string, a dot, the method name, and the arguments. Because a string cannot change, the original text is untouched, and the result is a new value that you print or store:
text = " Python is fun "
clean = text.strip() # new string; text is unchanged
print(clean.upper()) # PYTHON IS FUN
print(clean.find("is")) # 7
print(clean.split()) # ['Python', 'is', 'fun']
print(clean.startswith("Py")) # True
print("-".join(["a", "b", "c"])) # a-b-c
print(clean.isalpha()) # False Python String Methods Table
All method guides in this Python tutorial series (47 tutorials), grouped by purpose.
Change the case
6 methodsReturn a copy of the text in upper, lower, title, or swapped case. The original string never changes.
| Method | Returns | Description | Tutorial |
|---|---|---|---|
capitalize() | str | First character in upper case, the rest in lower case. | Try it |
casefold() | str | Aggressive lowercase for caseless comparisons. | Try it |
lower() | str | Return a copy of the string with every letter in lower case. | Try it |
swapcase() | str | Return a copy with upper case letters made lower and lower case made upper. | Try it |
title() | str | Return a copy with the first letter of every word in upper case. | Try it |
upper() | str | Return a copy of the string with every letter in upper case. | Try it |
Align and pad
5 methodsFit text into a fixed width with spaces, a fill character, or leading zeros, and turn tabs into spaces.
| Method | Returns | Description | Tutorial |
|---|---|---|---|
center() | str | Center the text in a field of a given width. | Try it |
ljust() | str | Return the string left-aligned in a field of a given width, padded on the right. | Try it |
rjust() | str | Return the string right-aligned in a field of a given width, padded on the left. | Try it |
zfill() | str | Return the string padded on the left with zeros to a given width, keeping a leading sign. | Try it |
expandtabs() | str | Replace tab characters with spaces. | Try it |
Search and count
7 methodsFind where a substring is, count it, or test how the string starts and ends.
| Method | Returns | Description | Tutorial |
|---|---|---|---|
find() | int | Index of the first match, or -1 if it is missing. | Try it |
rfind() | int | Return the index of the last match of a substring, or -1 if it is missing. | Try it |
index() | int | Index of the first match, or ValueError if it is missing. | Try it |
rindex() | int | Return the index of the last match of a substring, or raise ValueError if it is missing. | Try it |
count() | int | Count how many times a substring appears. | Try it |
startswith() | bool | Return True if the string starts with a given prefix, or any prefix in a tuple. | Try it |
endswith() | bool | Check whether the string ends with a suffix. | Try it |
Check the content
12 methodsReturn True or False to say what kind of characters the string holds, such as letters, digits, spaces, or ASCII only.
| Method | Returns | Description | Tutorial |
|---|---|---|---|
isalnum() | bool | True if every character is a letter or a digit. | Try it |
isalpha() | bool | True if every character is a letter. | Try it |
isascii() | bool | True if every character is in the ASCII range (code points 0 to 127). | Try it |
isdecimal() | bool | True if every character is a decimal digit. | Try it |
isdigit() | bool | True if every character is a digit. | Try it |
isidentifier() | bool | True if the string is a valid Python identifier, such as a variable name. | Try it |
islower() | bool | True if all letters are lower case and there is at least one letter. | Try it |
isnumeric() | bool | True if every character is a numeric character, including fractions and superscripts. | Try it |
isprintable() | bool | True if every character is printable, with no tabs, line breaks, or control characters. | Try it |
isspace() | bool | True if the string is not empty and holds only whitespace characters. | Try it |
istitle() | bool | True if every word starts with a capital letter and the other letters are lower case. | Try it |
isupper() | bool | True if all letters are upper case and there is at least one letter. | Try it |
Trim the edges
5 methodsRemove spaces, chosen characters, or a known prefix or suffix from the ends of a string.
| Method | Returns | Description | Tutorial |
|---|---|---|---|
strip() | str | Return a copy with spaces, or chosen characters, removed from both ends. | Try it |
lstrip() | str | Return a copy with leading whitespace, or chosen characters, removed from the left end. | Try it |
rstrip() | str | Return a copy with trailing whitespace, or chosen characters, removed from the right end. | Try it |
removeprefix() | str | Return a copy without the given prefix, if the string starts with it. Python 3.9+. | Try it |
removesuffix() | str | Return a copy without the given suffix, if the string ends with it. Python 3.9+. | Try it |
Split and join
6 methodsCut a string into a list of pieces, cut it at a separator into three parts, or glue many strings into one.
| Method | Returns | Description | Tutorial |
|---|---|---|---|
split() | list | Break a string into a list of pieces, at spaces or at a separator you choose. | Try it |
rsplit() | list | Break a string into a list of pieces, splitting from the right. | Try it |
splitlines() | list | Break a string into a list of lines, at line breaks. | Try it |
partition() | tuple | Split a string at the first separator into a tuple of three parts: before, separator, after. | Try it |
rpartition() | tuple | Split a string at the last separator into a tuple of three parts: before, separator, after. | Try it |
join() | str | Join the strings of an iterable into one string, with the separator between them. | Try it |
Replace and translate
3 methodsSwap one piece of text for another, or map single characters with a translation table.
| Method | Returns | Description | Tutorial |
|---|---|---|---|
replace() | str | Return a copy with every occurrence of a substring replaced by another one. | Try it |
maketrans() | dict | Build a translation table for translate() from characters to replace or delete. | Try it |
translate() | str | Return a copy with each character mapped through a translation table. | Try it |
Format and encode
3 methodsBuild text from templates and values, or turn a string into bytes.
String Operators and Idioms
These are not named methods, but you will use them next to the methods above in almost every program:
| Code | What it does |
|---|---|
a + b | Join two strings into a new string. |
text * 3 | Repeat the string three times. |
"py" in text | True if the substring appears anywhere. The simplest yes or no search. |
len(text) | Number of characters. |
text[0], text[-1] | The first and the last character. |
text[2:5] | A slice: the characters from index 2 up to, but not including, 5. |
text[::-1] | A reversed copy of the string. |
f"Hi {name}" | An f-string: embed values and expressions in a template. |
"%s is %d" % (name, age) | Old-style % formatting. |
for ch in text | Loop over the characters one at a time. |
str(42) | Turn another value into text. |
ord("A"), chr(65) | Convert between a character and its code point. |
sorted(text) | A new list with the characters in sorted order. |
text == other | Compare two strings. The comparison is case-sensitive. |
name = "Ada"
print(name + " Lovelace") # Ada Lovelace
print(name * 3) # AdaAdaAda
print("d" in name, len(name)) # True 3
print(name[0], name[-1]) # A a
print(name[::-1]) # adA
print(f"Hi {name}!") # Hi Ada!
print("%s is %d" % (name, 36)) # Ada is 36 Use in when you only need a yes or no answer, and find() when you also need the position. Build messages with an f-string or format(), and join many pieces with join() instead of adding them one by one with +.
Frequently Asked Questions
Start with capitalize()
Open the first guide in the series and work your way through the string methods.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
16 people found this page helpful
