Substrings in Python and the Many Ways You Can Use Them
Python, like many other programming languages, has a string type defined as a sequence of characters that could contain the standard letter characters in text, numeric characters, and special characters. An example of a string is “My address is 123 Main Street,” but just what is a Python substring? This article will introduce you to the concept of a substring, show you multiple ways to extract a substring from a string, and show you how to determine if a substring is present.
What is a Python substring?
If a string is a sequence of characters, a Python substring is simply a sequence of characters within another string. If our string is “I went to the store,” then both “went” and “to the s” are substrings of this string.
Software developers deal with substrings all the time. They might have a bunch of addresses as strings that end in a five-digit zip code that they want to extract. Or they might want to find a specific make and model of a car in a list of VIN numbers by searching for a substring that exists in that model’s VIN.
Last Updated July 2023
Learn Python like a Professional Start from the basics and go all the way to creating your own applications and games | By Jose Portilla, Pierian Training
Explore CourseSlicing strings in Python
You can use string slicing in Python in many ways to create a substring. Here is the slice syntax for Python strings:
string[start:stop:step] |
Here is what each of these are:
- string: The string being sliced.
- start: The starting index of the substring. If it is not included, the default value is 0.
- stop: The ending index of the substring. If it is not included or the value is larger than the length of the string, the default value will be the length of the string.
- step: The step of the slicing. If it is not included, the default value is 1.
Don’t worry about memorizing these values. You can see them in action in the Python programming examples below.
Get the rest of the string with just a start index
If you are only using a start index when slicing a string, it will return the rest of the string. Here is an example:
string = “All around the world” # Start at index 3 substring = string[3:] print(substring) # Prints “around the world” |
Get the start of a string with just a stop index
If you don’t use a start index and only set the stop index when slicing a string with Python, you will get the first part of the string. Here is an example:
string = “All around the world” # Stop at index 3 substring = string[:3] print(substring) # Prints “All” |
Top courses in Python
Get the middle of a string by using both a start and a stop index
If you want to remove some of the characters from the start of a string and some from the end, you will have to set both indexes. Here is an example:
string = “All around the world” # Stop at index 3 substring = string[4:10] print(substring) # Prints “around” |
Using a negative index when slicing a string
If you use a negative number as an index, your string will be sliced from the end rather than the beginning. Here are a couple of examples:
string = “The quick brown fox” # Get the last two characters of the string substring1 = string[-2:] print(substring1) # Prints “ox” # Stop at the fourth character from the end substring2 = string[:-3] print(substring2) # Prints “The quick brown” |
Get one character from a string
If you use Python string slicing and only use a single number without colons, it will return the character at that index. Here is an example:
string = “0123456789” # Get a character at this index substring = string[4] print(substring) # Prints “4” |
Get the whole string by slicing
Doing this is kind of useless, but it does demonstrate that using slice syntax without setting any values will return the complete string:
string = “My name is string” substring = string[::] print(substring) # Prints “My name is string” |
Get every other character in a string
The step value in Python string slicing may be hard to understand until you use it. It is basically how the slicing method steps through the string. By default, it is one and counts every character. If we make it 2, we can include every other character in our substring. Here is an example:
string = “Hello World” # Set step to 2 substring = string[::2] print(substring) # Prints “HloWrd” |
Reverse a string by slicing
Here is another way you can use step. You can use it to reverse a string. Here is an example;
string = “my string” # Use a negative step to reverse a string string_reversed = string[::-1] print(string_reversed) # Prints “gnirts ym” |
Searching for a substring in a string in Python
Instead of creating a substring from another string, you may want to determine if a substring exists in another string. You can do this in Python in several ways.
Using the in operator
The in operator will return True if a substring exists in a string and False if it does not. Here is an example:
# The string string = “Hello World, this is a string” # The substring we are looking for substring = “this” if string != None and substring in string: print(“Found the substring!”) else: print(“Could not find the substring.”) |
The in operator uses the __contains__ method of the Python str class and is not null safe, so if our string does not exist, an exception is thrown when we use it. This is why we first check that the string is not equal to None before we check if it contains the substring.
Using the index() method
The Python string class has an index() method that will return the index of the substring if it finds it in the string. This is useful when you need to know the position of the substring. Here is an example of how you can use this method:
# The string string = “Hello World, this is a string” # The substring we are looking for substring = “this” try: if string.index(substring): print(“Found the substring!”) except ValueError: print(“Could not find the substring.”) |
The check for the substring is wrapped in a try except block because the index method will throw a ValueError if the substring is not found.
Using the find() method
The find() method of the Python string class will also return the starting index of a substring if it is found, but it is more convenient because it won’t throw an exception if the string is not found. Instead, it returns a -1, because the indexes of a string start from 0. Here is how you would use the find method to determine if a string contains a substring:
# The string string = “Hello World, this is a string” # The substring we are looking for substring = “this” if string.find(substring) != -1: print(“Found the substring!”) else: print(“Could not find the substring.”) |
Using regular expressions
You can always use regular expressions to find a substring in a string in Python, and it will work, but it is more useful to match a specific pattern of characters in a string rather than a whole substring. The search method of the Python re module will return True if a substring exists in a string and False if it does not. Here is how you can use regular expressions in Python:
# Import search from the Python re module from re import search # The string string = “Hello World, this is a string” # The substring we are looking for substring = “this” if search(substring, string): print(“Found the substring!”) else: print(“Could not find the substring.”) |
You can see from all the examples that using substrings in Python is powerful, especially when you are doing a lot of text processing in your code. Python is definitely a useful and popular programming language. To help build your skills in Python, Udemy has a large selection of Python courses suitable for beginners as well as more advanced courses.