Python String Methods

Beginner
6 min read
Updated: Nov 2026
47 method guides
Method index

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().

Forty-seven methods, one rule.

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:

Python
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 methods

Return a copy of the text in upper, lower, title, or swapped case. The original string never changes.

MethodReturnsDescriptionTutorial
capitalize()strFirst character in upper case, the rest in lower case.Try it
casefold()strAggressive lowercase for caseless comparisons.Try it
lower()strReturn a copy of the string with every letter in lower case.Try it
swapcase()strReturn a copy with upper case letters made lower and lower case made upper.Try it
title()strReturn a copy with the first letter of every word in upper case.Try it
upper()strReturn a copy of the string with every letter in upper case.Try it

Align and pad

5 methods

Fit text into a fixed width with spaces, a fill character, or leading zeros, and turn tabs into spaces.

MethodReturnsDescriptionTutorial
center()strCenter the text in a field of a given width.Try it
ljust()strReturn the string left-aligned in a field of a given width, padded on the right.Try it
rjust()strReturn the string right-aligned in a field of a given width, padded on the left.Try it
zfill()strReturn the string padded on the left with zeros to a given width, keeping a leading sign.Try it
expandtabs()strReplace tab characters with spaces.Try it

Search and count

7 methods

Find where a substring is, count it, or test how the string starts and ends.

MethodReturnsDescriptionTutorial
find()intIndex of the first match, or -1 if it is missing.Try it
rfind()intReturn the index of the last match of a substring, or -1 if it is missing.Try it
index()intIndex of the first match, or ValueError if it is missing.Try it
rindex()intReturn the index of the last match of a substring, or raise ValueError if it is missing.Try it
count()intCount how many times a substring appears.Try it
startswith()boolReturn True if the string starts with a given prefix, or any prefix in a tuple.Try it
endswith()boolCheck whether the string ends with a suffix.Try it

Check the content

12 methods

Return True or False to say what kind of characters the string holds, such as letters, digits, spaces, or ASCII only.

MethodReturnsDescriptionTutorial
isalnum()boolTrue if every character is a letter or a digit.Try it
isalpha()boolTrue if every character is a letter.Try it
isascii()boolTrue if every character is in the ASCII range (code points 0 to 127).Try it
isdecimal()boolTrue if every character is a decimal digit.Try it
isdigit()boolTrue if every character is a digit.Try it
isidentifier()boolTrue if the string is a valid Python identifier, such as a variable name.Try it
islower()boolTrue if all letters are lower case and there is at least one letter.Try it
isnumeric()boolTrue if every character is a numeric character, including fractions and superscripts.Try it
isprintable()boolTrue if every character is printable, with no tabs, line breaks, or control characters.Try it
isspace()boolTrue if the string is not empty and holds only whitespace characters.Try it
istitle()boolTrue if every word starts with a capital letter and the other letters are lower case.Try it
isupper()boolTrue if all letters are upper case and there is at least one letter.Try it

Trim the edges

5 methods

Remove spaces, chosen characters, or a known prefix or suffix from the ends of a string.

MethodReturnsDescriptionTutorial
strip()strReturn a copy with spaces, or chosen characters, removed from both ends.Try it
lstrip()strReturn a copy with leading whitespace, or chosen characters, removed from the left end.Try it
rstrip()strReturn a copy with trailing whitespace, or chosen characters, removed from the right end.Try it
removeprefix()strReturn a copy without the given prefix, if the string starts with it. Python 3.9+.Try it
removesuffix()strReturn a copy without the given suffix, if the string ends with it. Python 3.9+.Try it

Split and join

6 methods

Cut a string into a list of pieces, cut it at a separator into three parts, or glue many strings into one.

MethodReturnsDescriptionTutorial
split()listBreak a string into a list of pieces, at spaces or at a separator you choose.Try it
rsplit()listBreak a string into a list of pieces, splitting from the right.Try it
splitlines()listBreak a string into a list of lines, at line breaks.Try it
partition()tupleSplit a string at the first separator into a tuple of three parts: before, separator, after.Try it
rpartition()tupleSplit a string at the last separator into a tuple of three parts: before, separator, after.Try it
join()strJoin the strings of an iterable into one string, with the separator between them.Try it

Replace and translate

3 methods

Swap one piece of text for another, or map single characters with a translation table.

MethodReturnsDescriptionTutorial
replace()strReturn a copy with every occurrence of a substring replaced by another one.Try it
maketrans()dictBuild a translation table for translate() from characters to replace or delete.Try it
translate()strReturn a copy with each character mapped through a translation table.Try it

Format and encode

3 methods

Build text from templates and values, or turn a string into bytes.

MethodReturnsDescriptionTutorial
format()strFill {} placeholders with values.Try it
format_map()strFill placeholders from a mapping such as a dict.Try it
encode()bytesEncode the string into bytes.Try it

String Operators and Idioms

These are not named methods, but you will use them next to the methods above in almost every program:

CodeWhat it does
a + bJoin two strings into a new string.
text * 3Repeat the string three times.
"py" in textTrue 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 textLoop 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 == otherCompare two strings. The comparison is case-sensitive.
Python
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

They are the functions you call on a str with dot notation, such as text.upper() or text.split(","). They change case, search, check, trim, split, join, replace, and format text.
This index covers all 47 public str methods, from capitalize() and casefold() to upper() and zfill().
No. A str is immutable, so every method returns a new string, list, number, or bool. Assign the result to a variable if you want to keep it.
Both return the position of a substring. find() returns -1 when the text is missing, while index() raises ValueError.
Start with upper(), lower(), strip(), split(), join(), replace(), find(), startswith(), and format() or f-strings. They cover most everyday text work.

Start with capitalize()

Open the first guide in the series and work your way through the string methods.

capitalize() tutorial →

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful