Python Substrings: Everything You Need to Know by@davisdavid

Python Substrings: Everything You Need to Know

Davis David HackerNoon profile picture

Davis David

Data Scientist | AI Practitioner | Software Developer. Giving talks, teaching, writing.

In python, a String is a sequence of characters that may contain special characters or alphanumeric characters, for example, "we meet on Friday at 08:00 am". It is possible to access sub-parts of the string commonly known as a python substring.

You can also define substring as a sequence of characters within a string. From the previous example, python substrings can be "Friday", "at", "meet", and others.

Python provides different ways and methods to generate a substring, check if a substring is present, index of a substring, and more.

You can extract a substring from a string by slicing with indices that obtain your substring as follows:


  • start - The starting index of the substring.
  • stop - The ending index of a substring.
  • step - A number specifying the step of the slicing. The default value is 1.

Indices can be positive or negative numbers. The positive indices start from the beginning to the end of the string and the negative indices start from the end to the beginning of a string.

In this article, you will learn various operations related to substrings in python.

1. Get the first n characters from the string 

This example will show you how to slice the first 5 characters from the string.

string = "hello world"

Here you define the stop index which is 5. The start index is by default is 0.


2.Get the middle characters from the string via python substrings

This example will show you how to slice characters from index 3 to index 5 from the string.

string = "hello world"


3.Get the last character from the string

To get the last character use the -1 index (negative index). Check the following example.

string = "hacker noon"


4.Get the last n characters from the string

In this example, you will slice the last 4 characters from the string. Here you use the negative index to start slicing from the end of the string.

string = "hackernoon"


5.Slice the string with steps via python substrings

You can slice the string with steps after indicating start-index and stop-index. By default, the step is 1 but in the following example, the step size is 2.

string = "welcome to hackernoon"

‘wloet akron’

6.Validate if a substring is present within a string

Sometimes you want to check if a substring present in a string. The following example will validate if a substring ‘noon’ is in a  string.

substring = "noon"
string = "welcome to hackernoon"
print(substring in string)

If present, it will return True otherwise False.


7.Alternative way to check if the python substring is present in a string 

You can use the find() method to check if a substring present in a string.

Let’s check the following example.

substring = "zz"
string = "hello world"

If available it returns the left-most index of the substring otherwise return -1 means not available.


This means “zz” is not present in “hello world”.

8.Get the character of a given index.

You can choose to slice a specific character according to its index number.

string ="hello world"


9. Create a list of substrings from a string in python

You can use the split() method to create a list of substrings. Let’s check the following example.

string = "welcome to hackernoon platform"

['welcome', 'to', 'hackernoon', 'platform']

10.Reverse the string with negative steps

To reverse the string, the step must be a negative value. Example -1.

string = "welcome to hackernoon"

‘noonrekcah ot emoclew’

11.Count substring occurrence in the string

You can use the count() method to know how many times a particular substring presented in a string.

string = "we will have a quick introduction to hackernoon this afternoon"


Final Thoughts on Python Substrings

Congratulations 👏👏, you have made it to the end of this article! I hope you have learned something new about python substring.

If you learned something new or enjoyed reading this article, please share it so that others can see it. Until then, see you in the next post!

You can also find me on Twitter @Davis_McDavid.

And you can read more articles like this here.

Want to keep up to date with all the latest in python? Subscribe to our newsletter in the footer below!


Signup or Login to Join the Discussion


Related Stories