This article aims to bring the learner to a better understanding of substrings in Python.

It will teach you how to create a substring, extract different portions of it from a string, and how substrings in Python work by using several examples to implement this. We will further check for the substring’s existence in a string.

Before we begin, it’s essential to know what a string and substring are. A string is a sequence of Unicode characters consisting of alphanumeric characters and/or special characters. We can define a substring as a portion of the sequence of characters within a string.

We will discuss the following:

  • Creating a substring.
  • Ways of slicing a substring.
  • Number of occurrences of a substring
  • Finding the presence of a substring in a string.

Let’s begin!

Creating a Substring

A substring is created mainly using the slicing technique. This technique allows the extraction of the substring by defining the delimiters of the begin-index, end-index, and step. These delimiters enable the extraction by specifying the precise index of the characters you want to obtain.

The Syntax looks like this:

string[begin-index: end-index: step]

Note that the index value in any string starts from zero.

The begin-index parameter indicates the substring’s starting index. Therefore, if you omit this element when slicing, Python automatically assumes its index value is zero.

End-index is the substring’s last index. When you do not mention it, slicing assumes its value equals the string’s length.

Step: It indicates the next character to consider after the current character. The value of this parameter is usually one. When the step parameter is omitted during slicing, its value is still assumed to be one.

Ways of slicing a string

There are several ways we can obtain a substring from a string. These include:

#1. Slicing using begin-index and end-index.

String = string[begin-index : end-index]

In an instance you want to obtain the first name of a person from their full name, you will implement it like this:

string = 'Michael Doe'

print(string[0:7])

The output will be:

Michael

#2. Slicing using begin-index without end-index.

String= string[begin-index:]

Here, we only indicate from what index we want to start extracting our substring characters from the string—slicing extracts until the last index of the entire string that usually has an index of -1.

Example:

string = 'This is to demonstrate slicing of a string using the begin-index without the end-index'

print(string[7:])

Output:

to demonstrate slicing of a string using the begin-index without the end-index

#3. Slicing using the end-index without the begin-index.

String = string[:end-index]

Here, we indicate the last character the substring should include, leaving out from where it should begin. Therefore, slicing will display the substring starting the index zero character of the string.

Example:

string = 'This is to demonstrate slicing of a string using the end-index without the begin-index'

print(string[:9])

Output:

This is t

#4. Slicing the entire string.

String = string[ : ]

In this case, we do not identify any of the delimiters; therefore, the entire string is returned.

Example

string = 'This is to demonstrate slicing of the entire string'

print(string[:])

Output:

This is to demonstrate slicing of the entire string

#5. Slicing a single character from a string.

String = string[index]

A precise index value of a character of the string is sliced out here.

Example:

string = 'This is to demonstrate slicing of a single character from a string'

print(string[8])

The ‘t’ in ‘to’ is printed since its index value is 8.

#6. Slicing using the begin-index, end-index, and step.

Example

string = 'This is to demonstrate slicing of the string using the begin-index, step, and end-index'

print(string[11:24:1])

When the step value is set to 1, normal slicing occurs, and the following is output here.

demonstrate s

Using the same example, let us set the step value to 2.

string = 'This is to demonstrate slicing of the string using the begin-index, step, and end-index'

print(string[11:24:2])

Slicing of the characters will be done in steps of 2 like this.

dmntaes

Slicing extracts the second character after the current one. Therefore, Python slices’ demonstrate s’ to D-m-n-t-a-e-s.

#7. Reversing a string using a negative step.

In this example, the entire string will be displayed from the last character of the string to the first one.

string = 'This is to demonstrate reversing string using the negative step'

print(string[::-1])

Output:

pets evitagen eht gnisu gnirts gnisrever etartsnomed ot si sihT

The negative step works like this:

R E V E R S E
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1

The start index value of the reverse string above starts from 0 by default and ends in 6. The negative step reverses the index value of the last character and applies it to the first character, and negates this value.

More examples on slicing

Obtaining the first four substring characters in a string.

string = 'characters'

print(string[0:4])

Will output:

char

Finding the presence of a substring in a string

Python uses either the find() function or the ‘in’ operator to check for a substring’s existence in a string.

Using the ‘in’ operator example

string = 'this is to demonstrate that a particular substring is found in a string '
if 'find' in string: 
    print('The substring is found') 
else: 
    print('Could not find the substring defined')

Output:

Could not find the substring defined

The example above checks whether the substring ‘find’ is present in the declared string. Since the substring is not found in the string, the output is as displayed above.

Replacing the substring ‘find’ with the substring ‘that’ and checking whether it exists in the string or not would return ‘substring is found’ instead because it is present in the string.

Using the find() function example:

Understanding Substrings in Python Development Python

string = 'using string to find if a substring is present in a string'

if string.find('found') != -1:

    print("The substring 'found' exists")

else:

    print("The substring 'found' does not exist")

output:

The substring 'found' does not exist

In the above example, we tried to find a substring that is not part of our string. As we have seen above, the find() function checks through the entire string, and is not found this particular ‘found’ substring, it returns the output ‘The substring found does not exist’ as the output.

Finding the number of occurrences of a substring

Python uses the count() function that implements this condition, as in the example below.

string = " Counting the number of occurrences of 'o' in this string "

print("The 'o' substring count is: ",string.count('o'));

Output:

The 'o' substring count is:  5

Conclusion

This article should give you a better understanding of what a substring is in Python, how to create it, and has clearly explained the idea of slicing and how to implement it. Using the examples provided above as guidelines, practice more examples to understand the concept better.

You may also learn how to create a number guessing game using Python.

Happy coding!