Substrings

MamtaWardhani's avatar
Published Mar 6, 2022Updated Apr 8, 2025
Contribute to Docs

A Python substring is a sequence of characters that are part of an original string. In Python, substrings can be obtained by using the slicing feature on a string variable. A slice can be made in a specific position within the string, or it can be made at the default index.

Syntax

A slice is made by using the open [ and closed ] square brackets next to a string variable. Inside the brackets, the position can be given:

string[start:end:step]
  • start defaults to 0 and gives the initial position the slice will start from.
  • end defaults to -1 and is the position where the slicing will end.
  • step defaults to 1 and indicates the number of steps to take in between indexes.

Examples

The following examples show different ways of obtaining substrings from an original string name.

name = "Code Ninja"

Retrieving Single Characters

When only one index is specified, a single character is returned. An index of 0 retrieves the first character of the string:

print(name[0])
# Output: C

Negative numbers work on the string backwards. For example, index -1 retrieves the last character of the string:

print(name[-1])
# Output: a

Negative Start Index

Using a negative start index (-n) with the default end value accesses the last n characters of the string. The following gives access to the last three characters of the string:

print(name[-3:])
# Output: nja

End Index

To specify only an end index, use [:n], where n is the ending position. This will return the first n characters.

print(name[:4])
# Output: Code

Negative Step Value

Given a negative step value, returns the results backward:

reversed = name[::-2]
print(reversed)
# Output: anNeo

Keyword in

The in keyword can be used to check for a specific substring, like in the example below:

print('de' in name)
# Output: True

.find() Method

The string method .find() can also be used to find a subset. It returns the index of the first occurrence of the substring. If the substring is not found, it returns -1.

Code
Output
Loading...

Best practices for using substrings in Python

  • Use slicing for efficiency: String slicing is optimized in Python and should be preferred over loops for extracting substrings.
  • Utilize in for membership checks: The in operator is the most efficient way to check if a substring exists within a string.
  • Normalize case before comparisons: Convert strings to lowercase or uppercase to avoid case-sensitive mismatches.
  • Handle missing substrings gracefully: Use .find() instead of .index() when searching for substrings to avoid exceptions.

FAQs

1. What happens if the end index is beyond the string length in slicing?

If the end index exceeds the string length, Python does not raise an error. It simply returns the available portion of the string.

2. How can I check if a substring exists within a string?

You can use the `in` operator or the `.find()` method. The `in` operator returns `True` or `False`, while `.find()` returns the starting index of the substring or `-1` if not found.

3. Why should I use `.find()` instead of `.index()`?

The `.find()` method returns `-1` if the substring is not found, whereas `.index()` raises an exception. If you don’t want to handle exceptions manually, `.find()` is a safer choice.

4. Can I use negative indexes for substring extraction?

Yes, Python allows negative indexing, which counts from the end of the string. You can use negative values for both the start and end positions when slicing.

All contributors

Contribute to Docs

Learn Python on Codecademy