How to Get a SubString from a String in Python?

In this python tutorial, you will learn how to get a substring from a string.

Table Of Contents

Introduction

A string is a set of characters. A substring is also a set of characters but it resides in a string. Let’s discuss some approaches to get a substring from a string.

Get a substring from a string by index position

We can select a substring from a string based on index positions or range of indices. Let’s see how to do that.

Get substring from a string through slicing

Here, we will return the substring by specifying index position range with the slice( [:] ) operator.

Advertisements

Syntax:

actual_string[first:end]

Where the actual_string is the input string. Also, the first variable specifies the first index position in string, from which the substring will start and the last variable is the last index position where substring will end.

Example:

In this example, we will get the substrings with the slice operator.

input_str="Welcome to thisPointer"

# Display the actual string
print(input_str)

# Print the substring from 1st to 4th position
print("Substring from Index 1 to Index 4: ", input_str[1:4])

# Print the substring from 1st to 8th position
print("Substring from Index 1 to Index 8: ", input_str[1:8])

# Print the substring from  4th position to 8th position
print("Substring from Index 4 to Index 8: ", input_str[4:8])

# Print the substring from   8th position to 12 th position
print("Substring from Index 8 to Index 12: ", input_str[8:12])

Output:

Welcome to thisPointer
Substring from Index 1 to Index 4:  elc
Substring from Index 1 to Index 8:  elcome 
Substring from Index 4 to Index 8:  ome 
Substring from Index 8 to Index 12:  to t

In the above code, we selected the substring from

  1. 1st to 4th position
  2. 1st to 8th position
  3. 4th position to 8th position
  4. 8th position to 12th position

Get substring containing first character only

It is possible to return only the first character from the actual string using index position. Index starts from 0 in the string. So, to select the first character from string using this syntax,

Syntax:

actual_string[0]

Where actual_string is the input string. It will return a substring containing only first character from the string.

Example:

In this example, we will get the first character from the actual string.

input_str="Welcome to thisPointer"

# Display the actual string
print(input_str)

# Get the first charcater
first_char = input_str[0]

print(first_char)

Output:

Welcome to thisPointer
W

It fetched a substring that contains the first character of the string.

Get substring containing last character only

It is possible to return only the last character from the actual string as a substring, using the index positions. Last character’s Index is -1. So, we can use the following syntax of string slicing to select the last character,

Syntax:

actual_string[-1]

Where actual_string is the input string. It will return a substring containing only the last character of string.

Example:

In this example, we will get the last character from the actual string.

input_str="Welcome to thisPointer"

# Display the actual string
print(input_str)

# Get the last charcater
last_char = input_str[-1]

print(last_char)

Output:

Welcome to thisPointer
r

It fetched a substring that contains the last character of the string.

Get a substring from a string using the regex module

The regex module supports string functions. Among that, split() is used to get the split the substrings from the string based on the delimiter.

Syntax:

re.split(" ", input_str)

It takes two parameters.

Parameters:
1. the first parameter is the delimiter where the string is split from the input_str.
2. input_str is the actual string.

It returns the splitted substrings in a list.

Example 1:

In this example, we will split the string based on the’ ‘ space delimiter into the list of sub-strings.

# Import the regex module
import re

# Consider the below string
inputStr="Welcome to thisPointer"

# Display the actual string
print(inputStr)

# Get the substrings
substrings = re.split(" ", inputStr)

print(substrings)

# get 2nd substring from the string
subStr = substrings[1]

print("Second Substring: ", subStr)

Output:

Welcome to thisPointer
['Welcome', 'to', 'thisPointer']
Second Substring:  to

We can see that string is split based on space delimiter and returned 3 substrings. Then we selected the second substring from that and printed it.

Example 2:

In this example, we will split the string based on the ‘&’ delimiter into the list of sub-strings.

# Import the regex module
import re

inputStr="Welcome&to&thisPointer"

# Display the actual string
print(inputStr)

# Get the substrings
substrings = re.split("&", inputStr)

print(substrings)

# get 2nd substring from the string
subStr = substrings[1]

print("Second Substring: ", subStr)

Output:

Welcome&to&thisPointer
['Welcome', 'to', 'thisPointer']
Second Substring:  to

We can see that string is split based on the ‘&’ delimiter and returned 3 substrings. Then we selected the second substring from that and printed it.

Summary

From this tutorial, welearned to get a substring from a string using index positions through a slice operator. We can return the first and last characters in a string using 0 and -1 index positions. If you want to return all the substrings from a string, you can use split() from the regex module. Happy Learning.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top