How To Substring In Python?


Python is a feature-rich programming language that provides a lot of string or text-related functions. String manipulation provides different operations where Substring operation is one of the most important.

What Is Substring?

Substring is an operation that will get some part of the specified string. Substring operations can be done in different ways and methods. For example “I love poftut.com” provides the substring “poftut.com” and “love” etc.

String Type Builtin Slicing

The most popular, easy and practical way to get substring is using the String data type slicing operator. Strings are like character arrays and every character has an index number. So providing these index number some part or the string or a substring can be restrived from a string.

SUBSTRING = STRING[START_INDEX:END_INDEX]

STRING is the text or string which is the source of the SUBSTRING and contains characters.

START_INDEX is the substring index start number where specifies the SUBSTRING first character. START_INDEX is optional and if not provided 0 is assumed.

END_INDEX is the substring index end number where specifies the SUBSTRING last character. END_INDEX is optional and if not provided the last character of the STRING is assumed.

SUBSTRING is the sub string which is returned with the START_INDEX and END_INDEX numbers from the STRING.

Substring From Specified Index To The End

Lets start with a simple example about substring where we will specify the start index of the substring and do not provide the end index which will be assumed as the last character of the given string.

s1 = "I love poftut.com"
 
s1[0:]
# The output is 'I love poftut.com'

s1[1:]
# The output is ' love poftut.com'

s1[2:]
# The output is 'love poftut.com'

s1[5:]
# The output is 'e poftut.com'

s1[55:]
# The output is ''
Substring From Specified Index To The End

We can see that when we provide start index as 0 the whole comple string is returned as substring. If we provide start index as 55 which do not exist for the given string the substring is empty.

LEARN MORE  strlen() Function In C and C++ Tutorial with Examples

Substring From Start To Specified Index

As the start index is optional we can only specify the end index for the substring. The start index will be set as 0 by default.

s1 = "I love poftut.com"

s1[:0]
# The output is ''

s1[:1]
# The output is 'I'

s1[:2]
# The output is 'I '

s1[:5]
# The output is 'I lov'

s1[:55]
# The output is 'I love poftut.com'
Substring From Start To Specified Index

Substring From Start Specified Index To Specified Index

Even both the start and end index is optional we can specify both of them. This will give us complete control over the substring where we can explicitly set the start and end index of the substring.

s1 = "I love poftut.com"


s1[0:16]
# The output is 'I love poftut.co'

s1[0:17]
# The output is 'I love poftut.com'

s1[5:17]
# The output is 'e poftut.com'

s1[5:7]
# The output is 'e '

1[7:5]
# The output is ''
Substring From Start Specified Index To Specified Index

Reverse Substring

Reverse substring is an operation where negative index numbers are used to specify the start and end index of the substring. Using negative number will reverse the index.

s1 = "I love poftut.com"


s1[5:]
# The output is 'e poftut.com'

s1[-5:]
# The output is 't.com'

s1[5:8]
# The output is 'e p'

s1[-5:-8]
# The output is ''
Reverse Substring

Substring with Specified Character By Using split() Method

split()isa string builtin functin which can split and create substrings from the given string. Split requires a split character which will be used like a splitter or delimiter. By default space ” ” is the split character but it can be also provided explicitly to the split() function.

s1 = "I love poftut.com"


s1.split()
# The output is ['I', 'love', 'poftut.com']

s1.split('t')
# The output is ['I love pof', 'u', '.com']

s1.split('.')
# The output is ['I love poftut', 'com']
Substring with Specified Character By Using split() Method

Leave a Comment