Home Python Python substring

Python substring

The term “string” refers to a collection of Unicode characters. A string is a collection of characters that can include both alphanumeric and special characters. We will learn about substring in Python and how it works in this article:

What is a substring in Python?

In Python, a substring is a sequence of characters contained a string within another string. In Python, it’s also known as string slicing. Substringing a string in Python can be done in a variety of ways. Thus, it’s commonly referred to as slicing.’ In this demo, we will follow the following template.

string[begin: end: step]

Where,

  • begin – The index at which the substring begins. The substring has the character at this index. If begin isn’t specified, it’s considered to be 0.
  • end: The substring’s finishing index. The substring does not include the character at this index. If end is omitted, or if the supplied value is more than the string length, it is presumed to be equal to the string length by default.
  • step: After the current character, there has to be an inclusion of every ‘step’ character. 1 is the default value. If the step value isn’t specified, it’s considered to be 1.
  • Get all characters from the beginning index to end-1 using the template string[begin:end].
  • Get all characters from the beginning of the string to end-1 using string[:end].
  • string[start:]: Get all characters from the string’s index start to the end.
  • Get all characters from start to end-1, ignoring every step character. string[start:end:step]: Get all characters from begin to end-1, ignoring every step character.

Examples:

string = "Codeunderscored"
print(string[0:5])

Note that print(string[:5]) yields the same output as print(string[:5]) (string[0:5])

From the third character in the string, create a four-character substring.

string = "Codeunderscored"
print(string[2:6])

Would you please keep in mind that the start or end index could be negative? When you use a negative index, you begin counting from the end of the string rather than the beginning (i.e., from right to left). The last character of the string is represented by index -1, the second to last character by index -2, etc.

Get the string’s last character.

string = "Codeunderscored"
print(string[-1])

Get the string’s last five characters

string = "Codeunderscored"
print(string[-5:])

Obtain a substring that contains all characters with the exception of the last four and the first.

string = "Codeunderscored"
print(string[1:-4])

Additional examples

str = "Codeunderscored"

print str[-5:-2] # prints 'eCa'
print str[-1:-2] # prints '' (empty string)

Get all the other characters in a string.

string = "Codeunderscored"
print(string[::2])

Check if a string contains a substring

In our quest of checking the presence of a substring in a string, we’ll use the in operator.

test_data = "Life comes in all dimensions, and you have to do is make alternative plans by being busy doing something."
'Life' in test_data
'plant' in test_data
Check if a string contains a substring
Check if a string contains a substring

Why does indexing indicate an index out-of-range error while slicing does not?

index_error = 'Life'
index_error[5]

Try to return the value at index 5 with index error[5]. However, if the value at index 5 is not available, an error will be displayed. The sequence of values is returned by slicing. As a result, if the value at index[5] is missing, Python will not throw an exception.
Always use slicing if you are unsure of the length of text data.

Why does indexing indicate an index out of range error
Why does indexing indicate an index out of range error

Using list comprehension to retrieve all substrings

# Get all substrings of string using list comprehension and string slicing
s= 'PYTHON'
str=[s[i: j]
for i in range(len(s))
for j in range(i + 1, len(s) + 1)]
print (str)

 

Output: ['P', 'PY', 'PYT', 'PYTH', 'PYTHO', 'PYTHON', 'Y', 'YT', 'YTH', 'YTHO', 'YTHON', 'T', 'TH', 'THO', 'THON', 'H', 'HO', 'HON', 'O', 'ON', 'N']

Using the itertools.combinations() technique to retrieve all substrings

# Get all substrings of string using list comprehension and string slicing

s= 'PYTHON'
str=[s[i: j]
for i in range(len(s))
print (str)

 

Output: ['P', 'PY', 'PYT', 'PYTH', 'PYTHO', 'PYTHON', 'Y', 'YT', 'YTH', 'YTHO', 'YTHON', 'T', 'TH', 'THO', 'THON', 'H', 'HO', 'HON', 'O', 'ON', 'N']

Check to see if a substring exists within another string.

Putting in the operator

#Check if the string is present within another string using in operator

original_string= 'This is to demonstrated substring functionality in python.'
print("This" in original_string)
print("Not present" in original_string)

 

Output:

True

False

If a string contains a particular substring, the in operator returns true; otherwise, it returns false.

Using the find method

Check if string is present within another string using find method
s= 'This is to check for a substring instance in python.'
print(s.find("check"))
print(s.find("Absent"))
Output:

11

-1
Using the find method
Using the find method

If a substring exists within another string, the Find method will print the index; otherwise, if a string does not exist, it will return -1.

Determine if a string contains a substring

The operator for in

The in operator is the simplest way to see if a Python string contains a substring.

In Python, the operator used in the verification of membership in data structures is in. It gives you a Boolean value (either True or False). In Python, we can use the in operator to see if a string contains a substring by invoking it on the superstring:

fullstring = "Codeunderscored"
substring = "scored"

if get_substring in originalstring:
  print("Found!")
else:
  print("Not found!")
use in operator to check if string contains a substring
use in operator to check if a string contains a substring

This operator is a shortcut for executing an object’s contains method, and it can also be used to see if an item in a list exists. It’s worth mentioning that it’s not null-safe. Thus an exception would be thrown if our fullstring pointed to None:

TypeError: argument of type ‘NoneType’ is not iterable

To avoid this, first determine whether it points to None or not:

fullstring = None
substring = "scored"

if fullstring != None and substring in fullstring:
  print("Found!")
else:
  print("Not found!")

The String.index() Method

String.index() is a method that returns the index of a string.

The index() function of the String type in Python can be used to get the starting index of the initial cause of a substring within a given string.

A ValueError exception is thrown if the substring is not found, which can be managed with a try-except-else block:

fullstring = "Codeunderscored"
substring = "scored"


try:
  fullstring.index(substring)

except ValueError:
  print("Not found!")
else:
  print("Found!")

 

This approach is handy if you need to know the substring’s position within the whole text rather than merely its existence.

String.find() is a method that allows you to find something in a string.

The String type contains another method called to find that is more convenient to use than index() because it doesn’t require any exception handling.

If find() fails to discover a match, it gives -1; otherwise, it returns the substring’s left-most index in the bigger string.

fullstring = "Codeunderscored"
substring = "scored"

if fullstring.find(substring) != -1:
  print("Found!")
else:
  print("Not found!")

 

This approach should be preferred over index() if you want to avoid having to catch mistakes.

Regular Expressions (Regular Expressions) (RegEx)

Regular expressions allow you to check strings for pattern matching in a more flexible (but more sophisticated) method. A built-in module supports regular expressions in Python called re. Search() is part of the functions in the module called re – we can use to match a substring pattern:

from re import search

fullstring = "Codeunderscored"
substring = "scored "

if search(substring, fullstring):
  print "Found!"
else:
  print "Not found!"

 

If you need a more complex matching function, such as case insensitive matching, this way is best. Otherwise, for simple substring matching, the complexities and slower speed of regex should be avoided.

How to get the count of times that a substring exists in a string

You may count the number of times a term appears in a document using the find() and replace() methods. Please see the code below for further information.

text_data_test = " Life comes in all dimensions and you have to do is make alternative plans by being busy doing something."
count = 0
while 'life' in text_data_test:
  text_data_test = text_data_test.lower().replace('life','',1)
  count+=1
  f'occurrence of word life is {count}'

The word life appears 3 times if the lower() function is removed. Because replace() is case-sensitive. Also, change the replace() function’s inputs to see what happens.

The index of a string’s repeated substring

The find() method can be used to find all the substring indexes in a text or string.

text_data_test = " Life comes in all dimensions and you have to do is make alternative plans by being busy doing something."
index_list = []
flag = 0
count = 0
word_length = len('life')
while 'life' in text_data_test:
  return_index = text_data_test.lower().find('life')
  print(return_index)
  
if return_index == -1:
  break
  
if len(index_list) == 0:
  index_list.append(return_index)
else:
  index_list.append(return_index + word_length * count)
  text_data_test = text_data_test.lower().replace('life',"",1)
  print(text_data_test)
  count += 1

There is no built-in method in Python that returns the index of all repeated substrings in a string. As a result, we’ll have to construct a new one.

How to replace Substrings

The replace() method can be used to substitute a word or a collection of words.

text_data = "Life is what happens when you're busy making other plans."
text_data.replace('other','no')
text_data.replace('making other','with')

Get the last substring of a sentence, regardless of its length

Using the code below, you can get the last component of the string. It will also function with sentences of various lengths.

test_data1 = " Life comes in all dimensions and you have to do is make alternative plans by being busy doing something."
test_data2 = "Today is a beautiful day."
for i in [test_data1, test_data2]:
  print(i.split()[-1])

Using a word or a character, split a string into substrings

Imagine you have a business requirement that the application break a string into substrings if a busy word appears in it. You can do so by using the code below.

text_data = "Life is what happens when you're busy making other plans."

if 'busy' in text_data:
  print(text_data.split('busy'))

A single character can also be used to separate the string.

text_data = " Life comes in all dimensions and you have to do is make alternative plans by being busy doing something."
if '.' in text_data:
  print(text_data.split('.'))

Example 1:

# code to demonstrate to create a substring from a string

# Initialising string
ini_string = 'xbzefdgstb'

# printing initial string and character
print ("initial_strings : ", ini_string)

# creating substring from start of string
# define length upto which substring required
sstring_strt = ini_string[:2]
sstring_end = ini_string[3:]

# printing result
print ("print resultant substring from start", sstring_strt)
print ("print resultant substring from end", sstring_end)
Example-1-creating-a-substring
Example 1: creating a substring

Example 2:

We’ll explore how to make a string by extracting characters from a specific positional gap in this example.

# code to demonstrate to create a substring from string

# Initialising string
ini_string = 'xbzefdgstb'

# printing initial string and character
print ("initial_strings : ", ini_string)

# creating substring by taking element
# after certain position gap
# define length upto which substring required
sstring_alt = ini_string[::2]
sstring_gap2 = ini_string[::3]

# printing result
print ("print resultant substring from start", sstring_alt)
print ("print resultant substring from end", sstring_gap2)
Example-2: demonstrate to create a substring from string
Example-2: demonstrate to create a substring from a string

Example 3:

We’ll look at both taking a string from the middle and taking a string with a positional space between characters in this example.

Python3 code to demonstrate how to create a substring from a string

# Python3 code to demonstrate to create a substring from string

# Initialising string
ini_string = 'xbzefdgstb'

# printing initial string and character
print ("initial_strings : ", ini_string)

# creating substring by taking element
# after certain position gap
# in defined length
sstring = ini_string[2:7:2]

# printing result
print ("print resultant substring", sstring)
Example 3: demonstrate to create a substring from string
Example 3: demonstrate to create a substring from a string

Conclusion

A substring is a subsection of a longer string. You can use the slicing syntax to get a substring. In addition, slicing allows you to choose a character range to retrieve. A third argument can be used to skip over specific characters in a range.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More