cft

Python Substrings: Everything You Need to Know

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.


user

HackerNoon

3 years ago | 3 min read

This article was originally published by @davisdavid at Python Substrings: Everything You Need to Know

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:

string[start:stop:step]

  • 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"
print(string[:5])

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

Output:
‘hello’

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"
print(string[3:5])

Output:
'lo’

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"
print(string[-1])

Output:
‘n’

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"
print(string[-4:])

Output:
‘noon’

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"
print(string[::2])

Output:
‘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.

Output:
True

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"
print(string.find(substring))

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

Output:
-1

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"
print(string[4])

Output:
‘O’

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"
print(string.split())

Output:
['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"
print(string[::-1])

Output:
‘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"
print(string.count('noon'))

Output:
2

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.

This article was originally published by @davisdavid at Python Substrings: Everything You Need to Know

Upvote


user
Created by

HackerNoon

The place for programmers, startups, CEOs, and gamers to share their stories with the world.


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles