What is a Python Substring?

python pip

A Python substring is a part of a larger string. For example, “py” is a substring of “python”. Knowing how to get and work with substrings is very useful for handling text data.

It helps you extract specific parts of text. You can use it to get names from a full address, or extract specific data from a log file. This saves time and makes text processing easy.

What is a Python Substring?

A Python substring is a sequence of characters within another string. You do not create a separate data type for substrings. They are just regular strings that you extract from a larger one.

Python uses a technique called string slicing to get substrings. Slicing lets you pick a part of a string by defining a start and an end point.

How to Get Substrings (String Slicing)

You use square brackets [] with slice notation to get a substring. The general format is string[start:end:step].

  • start: The index where the substring begins. This index is included. (Default is 0 if not specified).
  • end: The index where the substring ends. This index is not included. (Default is the end of the string if not specified).
  • step: How many characters to jump. (Default is 1 if not specified).

Python uses zero-based indexing. This means the first character is at index 0, the second at 1, and so on.

Academy Pro

Python Programming Course

In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs.

11.5 Hrs
51 Coding Exercises
Learn Python Programming

Let’s look at examples.

1. Basic Slicing: string[start:end]

This is the most common way to get a substring. You specify a starting index and an ending index.

my_string = "hello world"

# Get characters from index 0 up to (but not including) index 5
sub1 = my_string[0:5]
print(f"Substring 1: '{sub1}'") 

# Get characters from index 6 up to (but not including) index 11
sub2 = my_string[6:11]
print(f"Substring 2: '{sub2}'") 

# Get a single character (from index 4 to 5)
sub3 = my_string[4:5]
print(f"Substring 3: '{sub3}'") 

This code outputs:

Substring 1: 'hello'
Substring 2: 'world'
Substring 3: 'o'

Remember, the end index is exclusive. The character at the end index is not part of the result.

2. Slicing to the End: string[start:]

If you omit the end index, Python extracts characters from the start index to the end of the string.

my_string = "python programming"

# Get substring from index 7 to the end
sub = my_string[7:]
print(f"Substring: '{sub}'") 

This code outputs:

Substring: 'programming'

3. Slicing from the Beginning: string[:end]

If you omit the start index, Python extracts characters from the beginning of the string up to (but not including) the end index.

my_string = "python programming"

# Get substring from the beginning up to (but not including) index 6
sub = my_string[:6]
print(f"Substring: '{sub}'") 

This code outputs:

Substring: 'python'

4. Full Copy: string[:]

If you omit both start and end indices, you get a full copy of the original string. This is useful when you want to make a copy without modifying the original.

my_string = "data science"

# Get a full copy
sub = my_string[:]
print(f"Substring: '{sub}'") 

This code outputs:

Substring: 'data science'

5. Using Negative Indices

Negative indices count from the end of the string. The last character is at index -1, the second to last at -2, and so on.

my_string = "example"

# Get the last character
last_char = my_string[-1]
print(f"Last character: '{last_char}'") 

# Get the last three characters
last_three = my_string[-3:]
print(f"Last three: '{last_three}'") 

# Get characters from index -5 up to (but not including) -2
part = my_string[-5:-2]
print(f"Part: '{part}'") 

This code outputs:

Last character: 'e'
Last three: 'ple'
Part: 'amp'

Negative indices are useful for extracting parts from the end of a string without knowing its exact length.

6. Slicing with a Step: string[start:end:step]

The step argument lets you skip characters. For example, a step of 2 means you take every second character.

my_string = "abcdefgh"

# Get every second character from the beginning to the end
every_second = my_string[::2]
print(f"Every second char: '{every_second}'") 

# Get characters from index 1 to 6, with a step of 2
sub_stepped = my_string[1:7:2]
print(f"Stepped substring: '{sub_stepped}'") 

This code outputs:

Every second char: 'aceg'
Stepped substring: 'bdf'

A common trick is using a negative step to reverse a string:

my_string = "reverse"

# Reverse the string
reversed_str = my_string[::-1]
print(f"Reversed string: '{reversed_str}'") 

This code outputs:

Reversed string: 'esrever'

Useful String Methods for Substrings

Besides slicing, Python offers methods to check for or find substrings.

1. in Operator: Check if a Substring Exists

You can use the in operator to check if a string contains a specific substring. It returns True or False.

main_string = "hello python"

# Check if "py" is in the string
has_py = "py" in main_string
print(f"'py' in string: {has_py}") 

# Check if "java" is in the string
has_java = "java" in main_string
print(f"'java' in string: {has_java}") 

This code outputs:

'py' in string: True
'java' in string: False

2. find() Method: Get the Starting Index

The find() method returns the lowest index where the substring is found. If the substring is not found, it returns -1.

main_string = "apple banana apple"

# Find the first occurrence of "apple"
index1 = main_string.find("apple")
print(f"Index of 'apple': {index1}") 

# Find "banana"
index2 = main_string.find("banana")
print(f"Index of 'banana': {index2}") 

# Try to find a non-existent substring
index3 = main_string.find("orange")
print(f"Index of 'orange': {index3}") 

This code outputs:

Index of 'apple': 0
Index of 'banana': 6
Index of 'orange': -1

You can also specify a start and end index for find() to search within a specific part of the string:

main_string = "apple banana apple"

# Find "apple" starting from index 1
index_after_first = main_string.find("apple", 1)
print(f"Index of 'apple' after index 0: {index_after_first}") 

This code outputs:

Index of 'apple' after index 0: 13

3. rfind() Method: Get the Last Starting Index

The rfind() method is like find(), but it returns the highest (rightmost) index where the substring is found.

main_string = "apple banana apple"

# Find the last occurrence of "apple"
last_index = main_string.rfind("apple")
print(f"Last index of 'apple': {last_index}") 

This code outputs:

Last index of 'apple': 13

4. count() Method: Count Occurrences

The count() method returns the number of non-overlapping occurrences of a substring in the string.

main_string = "banana split banana"

# Count occurrences of "banana"
num_bananas = main_string.count("banana")
print(f"Number of 'banana': {num_bananas}") 

# Count occurrences of "a"
num_a = main_string.count("a")
print(f"Number of 'a': {num_a}") 

This code outputs:

Number of 'banana': 2
Number of 'a': 7

Best Practices for Substrings

  • Understand Immutability: Python strings are immutable. When you get a substring, you are not changing the original string. You are creating a new string.
  • Use start:end for Clarity: For simple extractions, string[start:end] is clear and easy to read.
  • Negative Indexing for End Parts: Use negative indices when you need parts from the end of a string.
  • Avoid Out-of-Bounds Errors: Slicing does not raise an error if your start or end index is out of bounds. It just returns an empty string or adjusts to the string boundaries. This is safe. For example, my_string[100:200] on a 10-character string returns ''.
Avatar photo
Great Learning Editorial Team
The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.

Academy Pro Subscription

Grab 50% off
on Top Courses - Free Trial Available

×
Scroll to Top