How to Substring a String in Python

December 13, 2022


Python offers a range of functionalities to work with string data types. One such feature is the ability to extract substrings from a string.

Working with substrings is a valuable programming feature when dealing with textual data.

This guide teaches you how to substring a string in Python and the various use cases of substrings.

How to Substring a String in Python


What is a Substring in Python?

In Python, a substring is a character sequence inside a larger string, and every character has an index. The indexing can be:

  • Positive (from the first character to the last)
  • Negative (from the last character to the first)
Python string indexing

The syntax for extracting substrings from strings is:


Python uses string slicing notation to work with substrings, where:

  • start is the first character in the substring (default is the beginning).
  • stop is the first character excluded from the string (default is the end).
  • step is the distance between two characters (default is 1).

All three values can be positive or negative numbers. If left blank, the default values are assumed.

Alternatively, to divide a string into substrings based on a delimiter, use the split method:

string.split("<delimiter>","<max splits>")

Without a provided delimiter, the method divides the string based on whitespaces. By default, the maximum number of splits is unlimited, which you can limit only to perform a maximum number of divisions.

Substring Operations in Python

Python offers different ways to create substrings depending on the task. The examples in the sections below demonstrate various problems and walk through the code syntax to explain the behavior.

Create a Substring

To create a substring using slicing, refer to the following code:

quote = "Toto, I have a feeling we're not in Kansas anymore."

Python substring example output

The code prints a substring between two characters at the provided indexes.

Note: Learn how to perform string repetition in Python using several different methods.

Create a Substring From Beginning to Index

The following code shows how to make a substring from the beginning up to an index using slicing:

quote = "Toto, I have a feeling we're not in Kansas anymore."

Python substring beginning to index output

In both cases, the code prints a substring from the beginning. Use this method to fetch a substring before a specific character.

Create Substring from Index to End

To create a substring from a specific index to the end, see the following example:

quote = "Toto, I have a feeling we're not in Kansas anymore."

Python substring index to end output

In both cases, the code prints the end of a string after a specific character. Use negative indexing for substrings that are easier to count backward and regular indexing for substrings closer to the start.

Create Substrings Using Split

Use the split method to create substrings based on a delimiter. For example:

quote = "Toto, I have a feeling we're not in Kansas anymore."

Python split string into substrings output

The output shows the code splits the given string by spaces and stores the result into a list of individual substrings.

Check if a Substring Exists

To see whether a substring exists within a string, use the in operator and print the result:

quote = "Toto, I have a feeling we're not in Kansas anymore."

print("Toto" in quote)
check if substring in string python output

The code checks for the provided string and returns an appropriate message (True or False). The search performs an exact match and takes capitalization into account.

Count a Substring in a String

Use the count method on a string and provide the substring to find the number of occurrences in a substring. For example:

quote = "Toto, I have a feeling we're not in Kansas anymore."

Python count substrings in string output

The code counts the number of appearances of the letter "a" in the string. Use this method to find the total number of instances of a substring.

Find the Index of the First Substring

To fetch the index of the first found substring, use the following example:

quote = "Toto, I have a feeling we're not in Kansas anymore."

Python substring index first output

If the substring doesn't exist, the index method returns an error.

An alternative way to find the index of the first substring is:

quote = "Toto, I have a feeling we're not in Kansas anymore."

Python substring find first output

The find method returns -1 if the substring is not found.

Note: To find the last occurring index, use the rindex and rfind methods.

Find All Indexes of a Substring

To find the indexes of all occurring substrings, use a for loop and check with the startswith string method. For example:

quote = "Toto, I have a feeling we're not in Kansas anymore."

for index in range(len(quote)):
  if quote.startswith("a",index):
find all indexes of substring for loop output

The output prints all indexes of the substring in a new line.

Alternatively, use list comprehension to create a one-line solution:

quote = "Toto, I have a feeling we're not in Kansas anymore."

index_list = [index for index in range(len(quote)) if quote.startswith("a",index)]
find all indexes of substring list comprehension output

The resulting indexes are in a list. If there are no matches, the code returns an empty list.


You've learned how to create and work with substrings in Python. String slicing and various built-in methods simplify working with substrings. Knowing how to work with substrings is essential for tackling various text-based programming problems.

Was this article helpful?
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to PrettyPrint a JSON File with Python?
November 15, 2022

Learn how you can use Python to...
Read more
Python: How to Add Items to Dictionary
November 10, 2022

Learn how to add new items to a Python dictionary in...
Read more
File Handling in Python: Create, Open, Append, Read, Write
February 24, 2022

Working with files is part of everyday tasks in...
Read more
Handling Missing Data in Python: Causes and Solutions
July 1, 2021

Some machine learning algorithms won't work with missing data...
Read more