How to SubString a String in Python

Spread the love

Python offers numerous approaches to substring a string. It is frequently called “slicing.”

It follows this layout:

string[start: end: step]

What is a Substring in Python?

A substring is the piece of a string.

Python string gives different strategies to make a substring. Check if it contains a substring, file of the substring, and so on.

In this instructional exercise, we will investigate different tasks identified with substrings.

Slicing Strings in Python

Slicing using start-index and end-index

st= st[ startText : endText]

Please Note: index start from 0 in a string.

Start: The beginning list of the substring. The character at this record is remembered for the substring. If the start is excluded, it is accepted to equivalent to 0.

End: The ending list of the substring. The character at this record is excluded from the substring. If the end is eliminated or the predefined esteem surpasses the string length, it is thought to be equivalent to the distance as a matter of course.

Step: Every ‘progression’ character after the present character to be incorporated. The default esteem is 1. On the off chance that the progression esteem is discarded, it is accepted to equivalent to 1.

  • Template
    string[start:end]: Get all characters from index start to end-1
  • string[:end]: Get all characters from the beginning of the string to end-1
  • string[start:]: Get all characters from index start to the end of the string
  • string[start:end:step]: Get all characters from start to end-1 discounting every step character

Examples

Pick the first 4 characters of a string

string = “TechPeat” print(string[0:4])

Output:

> Tech

Get a substring of length 4 from the 3rd character of the string

string = “TechPeat” print (string[2:4])

Output:

> chPe

If it’s not too much trouble, note that the beginning or end list might be a negative number.

A negative file implies that you begin checking from the finish of the string rather than the start

Example from the option to left.

Record – 1 speaks to the last character of the string, – 2 speaks to the second to keep the going character, etc

Get the last character of the string

string = “TechPeat” print(string[-1])

Output:

> t

Get the last 4 characters of a string

string = “TechPeat” print(string[-4:])

Output:

> Peat

Get a substring that not contains 1st and last 3 characters from the string.

string = “TechPeat” print(string[1:-3])

Output:

> echP

Checking if the substring is found

s = ‘My Name is TechPeat’

if ‘TechPeat’ in s:

print(‘Substring TechPeat found’)

if s.find(‘TechPeat’) != -1:

print(‘Substring TechPeat found’)

We can utilize count() function to find the number of a substring in the string.

s = ‘My Name is TechPeat’

print(‘Substring count =’, s.count(‘a’))

s = ‘This Is The Best Tech Blog’
print(‘Substring count =’, s.count(‘Th’))

Output:

Substring count = 2
Substring count = 2

The slice object for getting the substring

There is a contrast between the all-encompassing cut and making a cutting article for getting a substring.

You may peruse the insights concerning both in the point by point instructional exercise.

This is how a cutting article is made:

slice(start ,stop ,step)

The cut build takes three files; start, stop, and step. These are isolated by commas and encased in the enclosure.

The accompanying model tells the best way to get a substring by utilizing the slice String:

 

slice_string = “The slice object to get substring”

obj_slice = slice(3, 17, 1)

print(slice_string[obj_slice])

Output:

slice object

 


Spread the love