- An Overview of Python String Methods
- But Wait – What Are Python String Methods Again?
- Change Text Case with str.lower() and str.upper()
- Normalize Text Case with str.capitalize() and str.title()
- Count Characters with str.count()
- Locate Characters and Words with str.find() and str.index()
- Modify Characters in the String with str.replace()
- Remove Surrounding Whitespace with str.strip()
- Split a String Into a List with str.split()
- Join a Sequence Into a Single String with str.join()
- Check if a String Has Letters or Numbers with str.isalpha(), str.isdigit(), and str.isalnum()
- Detect Starting or Ending Characters with str.startswith() and str.endswith()
- Combining Python String Methods
- Mastering String Methods in Python!
An Overview of Python String Methods
Python strings have a lot of functionality that you can leverage in your scripts. This includes common text operations like searching and replacing text, removing whitespace, or counting characters and words. Collectively, these functions are called Python string methods.
In this article, we’ll go through an overview of the main string methods available for Python scripts. If you want to do a deeper dive into Python string methods, check out our course Working with Strings in Python, which includes over 50 exercises and 10 hours’ worth of learning material.
But Wait – What Are Python String Methods Again?
Methods work much like plain old functions, but they are associated with a specific data type (e.g. integers, strings, or lists). You can think of methods as specialized functions that are designed to work with that particular type of data.
An important distinction between methods and functions is how to execute them. For regular functions, you simply use parentheses to execute it:
result = some_function(argument1, argument2)
Methods, on the other hand, must be accessed and executed from a variable. You do this by adding a period between the variable and the method name:
result = some_variable.some_method(argument1, argument2)
Notice the period before some_method ? This indicates that we are using a method. The data type of some_variable determines which methods are available.
Here’s a concrete example. We’ll define the variable name (a string) and then call the lower() method:
name = "John" result = name.lower()
If you’re confused about variables and functions, the Python Basics track will get you up to speed in no time.
Now that we know how methods work, let’s have an overview of the main Python string methods and what they are used for!
Important! In the examples below, we use the notation str.method_name() to indicate that these are string methods. When writing code, remember to replace str with the actual variable that you want to use when calling the method.
Change Text Case with str.lower() and str.upper()
Let’s start with two straightforward examples. The str.lower() and str.upper() string methods are used to change all the characters in a Python string to lowercase or uppercase, respectively. Everything else in the string, like punctuation and whitespace, remains the same. Take a look:
name = "Juliano" name_uppercase = name.upper() print(name_uppercase) # output: "JULIANO" text_all_caps = "THIS TEXT. IS WRITTEN IN ALL CAPS!" text_lowercase = text_all_caps.lower() print(text_lowercase) # output: "this text. is written in all caps!"
Python is smart enough to convert some language-specific characters (like «a» , «ë» , or «a» ) to their uppercase/lowercase counterparts:
special_characters = "N Ü Ł" print(special_characters.lower()) # output: "n ü ł"
Normalize Text Case with str.capitalize() and str.title()
The string methods str.capitalize() and str.title() are somewhat similar to str.lower() and str.upper() in that they modify characters to lowercase or uppercase. The difference lies in how they do it:
- str.capitalize() converts the first character of the Python string to uppercase and the remaining characters to lowercase (i.e. like you would capitalize a sentence).
- str.title() turns the first character of each word to uppercase and the remaining characters of each word to lowercase.
Let’s see these methods in action, starting with capitalize() :
fruits = "apple PEAR PeaCh oranGE" fruits_sentence_case = fruits.capitalize() print(fruits_sentence_case) # output: "Apple pear peach orange" fruits_title_case = fruits.title() print(fruits_title_case) # output: "Apple Pear Peach Orange"
Only the word “Apple» is capitalized when we use str.capitalize() , while with str.title() all fruits end up with their first letter capitalized. Check out this article for a more thorough discussion on how to capitalize words in Python.
Count Characters with str.count()
The str.count() method is used to count the characters in a Python string. You must provide the characters to count as the argument. Here’s an example where we count the number of «e» characters in a sentence:
sentence = "How many letters e are in this sentence?" num = sentence.count("e") print(num) # output: 7
You can count more than a single character. This is very helpful for counting the number of occurrences of a word in the string:
animals = "dog cat monkey dog rabbit dog" num = animals.count("dog") print(num) # output: 3
Note: Because Python is case sensitive, uppercase and lowercase letters are treated as different letters. For example, notice what happens when we when count for “x” in the following string:
sentence = "Python won't count these characters: X X X X X" num = sentence.count("x") # "x" and "X" are different characters print(num) # output: 0
Locate Characters and Words with str.find() and str.index()
As the name suggests, the str.find() method searches for a query string (the argument) in the template string:
template = "Python is a great programming language!" position = template.find("great") print(position) # output: 12
str.find() returns a number rather than a “found” or ”not found” value. This number is the index representing the position where the query string (here, “great”) starts in the template string. Remember that the first letter in a string has the index 0. If you’re unfamiliar with indices, our course about working with strings in Python will make the concept crystal-clear.
Indices can be used to get a substring from a string in Python. In the example below, we use it to select the text directly after the query string’s initial position:
template = "Python is a great programming language!" position = template.find("great") print(template[position:]) # output: "great programming language!"
str.find() returns -1 as a way to indicate that the query string wasn’t found:
template = "Python is a great programming language!" position = template.find("good") # there’s no "good" in the template print(position) # output: -1
The str.index() method is identical to str.find() , but it throws an exception instead of returning -1 when the query is not found:
template = "Python is a great programming language!" template.index("good") #Output: ValueError: substring not found
You may think that an error message is a bad thing, but sometimes it is helpful to interrupt the program immediately if the result isn’t found. Our Python Basics course will teach you how to properly handle exceptions so that your program doesn’t crash in these cases.
Modify Characters in the String with str.replace()
str.replace() is an easy way to replace characters in a Python string. Simply pass the characters to be replaced ( c1 ) and the characters to replace them ( c2 ) as arguments:
template = "Python is a great programming language!" c1 = "a" c2 = "x" replaced = template.replace(c1, c2) print(replaced) # output: "Python is x grext progrxmming lxnguxge!"
A common use for this string method is to remove whitespace from a string:
template = " look at this space! " replaced = template.replace(" ", "") # replace spaces with empty strings print(replaced) # output: "lookatthisspace!"
Remove Surrounding Whitespace with str.strip()
Another way to remove whitespace is using the str.strip() method. Unlike the previous example, it preserves the whitespace between words, only removing it at the beginning and end of the string:
template = " string with spaces " stripped = template.strip() print(stripped) # output: "string with spaces"
The str.strip() method has two variants, str.lstrip() and str.rstrip() , which respectively remove whitespace only from the left side (beginning) or the right side (end) of the string. You can see this behavior in the examples below:
template = " string with spaces " print(template.lstrip()) # output: "string with spaces " print(template.rstrip()) # output: " string with spaces"
Split a String Into a List with str.split()
The str.split() method is used to divide a string into a list of strings. By default, this is done by splitting the string at each whitespace:
animals = "dog cat monkey dog rabbit dog" animal_list = animals.split() print(animal_list) # output: ['dog', 'cat', 'monkey', 'dog', 'rabbit', 'dog']
You can also provide the character to split by as an argument. This is useful for splitting a line from a CSV file into a list of values. For example:
csv_line = "Jane,Doe,12,False,None" values = csv_line.split(",") # split at each comma character print(values) # output: ["Jane", "Doe", "12", "False", "None"]
This is just a simple example – if you’re interested in how to properly process data from a CSV file, take a look at How to Read a CSV File Into a List In Python or hop into our Read and Write CSV Files in Python course.
Join a Sequence Into a Single String with str.join()
The str.join() method is used to add a string between the elements of any Python sequence (i.e. a list, tuple, or another string). Here’s how it works:
template = "ABCDE" joined = "?".join(template) print(joined) # output: "A?B?C?D?E"
See what happened? We added the «?» character between each element (letter) of our template string. Some people find it confusing that the template is the argument instead of being the variable used to call the str.join() method, so watch out for that.
As mentioned, you can also use this string method to join elements from a list:
colors = ["Red", "Green", "Blue", "Yellow"] joined = "---".join(colors) print(joined) # output: "Red---Green---Blue---Yellow"
Check if a String Has Letters or Numbers with str.isalpha(), str.isdigit(), and str.isalnum()
The string methods str.isalpha() and str.isdigit() help you determine whether a string contains only letters or only numbers:
- str.isalpha() returns True if the string only has letters (no numbers or punctuation allowed) and False otherwise.
- str.isdigit() returns True if the string only has the numbers 0-9 in it and False otherwise.
Here are the methods in action:
s = "Hello" print(s.isalpha()) # output: True s = "Hello World!" # added punctuation and whitespace print(s.isalpha()) # output: False s = "42" print(s.isdigit()) # output: True s = "3.14" # a period is not a digit! print(s.isdigit()) # output: False
The str.isalnum() string method simply combines the two previous methods into one. It returns True if the string only has alphabet letters or digits in it:
s = "1jwk0a9f3q2jfd3s" print(s.isalnum()) # output: True s = "2j7xm5alw8qbh0. " # note the trailing question marks print(s.isalnum()) # output: False
Detect Starting or Ending Characters with str.startswith() and str.endswith()
Rather unsurprisingly, the Python string methods str.startswith() and str.endswith() are used to detect whether a string starts or ends with a given substring. Here is the str.startswith() method in action:
text = "I am learning about strings in Python!" print(text.startswith("I am")) # output: True print(text.startswith("You are")) # output: False
str.endswith() is particularly useful for detecting a file type by reading the file name’s extension:
filename = "my_data.xlsx" if filename.endswith(".xlsx"): print("I'm working with an Excel spreadsheet.") elif filename.endswith(".csv"): print("I'm working with a CSV file.") else: print("I'm working with something else entirely.")
If you run the code above, you’ll see the message «I’m working with an Excel spreadsheet.» (And by the way, we have an article on how to read Excel files with Python, along with a full course on reading and writing Excel files in Python.)
Combining Python String Methods
Python string methods really come together when you start using them for a specific task. For example, let’s say that we want to extract a list of words from a piece of text. We want to ignore any whitespace and punctuation in this process, and we also want to normalize the words by converting all characters to lowercase. We can do this by sequentially applying three different methods:
- Use str.lower() to transform the text to lowercase.
- Use str.replace() to replace periods with empty strings (effectively removing them from the string).
- Use str.split() to split the processed text into a list of words.
text = """ This is a very interesting text. IT HAS A SENTENCE IN UPPERCASE. And. It. Has. Another. Sentence. With. Lots. Of. Periods. """ processed_text = text.lower().replace(".", "") list_of_words = processed_text.split() print(list_of_words) # output: ['this', 'is', 'a', 'very', … etc
We end up with a single list of all lowercase words in the text – pretty cool! Note how we can even chain two methods together: the output from text.lower() becomes the input to the .replace() call. In a single line, we transform the text to lowercase and remove all periods.
Mastering String Methods in Python!
We just went through an overview of string methods in Python, but you’re only getting started! Go on to the working with strings in Python course if you want to practice everything you just read!