How to extract a substring from inside a string in Python?


In this article, we are going to find out how to extract a substring from inside a string in Python.

The first approach is by using Regular expressions. A string of characters that creates a search pattern is known as a regex, or regular expression. RegEx can be used to determine whether a string includes a given search pattern.

We will use re.search method of Regular Expressions and we will search for the given string that is given by the regular expression and we will extract it.

Example 1

In the example given below, we are taking a string as input and we are extracting the numerical substring of the string using the regular expression '(\$[0-9\,]*)

import re
str1 = 'The phone is priced at $15,745.95 and has a camera.'

print("The given string is")
print(str1)

print("The numeric substring is:")
res = re.search('(\$[0-9\,]*.[0-9]{2})', str1)
if res:
   print(res.group(1))

Output

The output of the above example is as given below −

The given string is
The phone is priced at $15,745.95 and has a camera.
The numeric substring is:
$15,745.95

Example 2

You can use group capturing in regular expressions to extract a substring from inside a string. You need to know the format and surrounding of the substring you want to extract. For example if you have a line and want to extract money information from it with the format $xxx,xxx.xx you can use the following 

import re
text = 'The phone is priced at $15,745.95 and has a camera.'
m = re.search('(\$[0-9\,]*.[0-9]{2})', text)
if m:
    print (m.group(1))

Output

This will give the output as follows −

$15,745.95

Note  The actual regex will depend on the conditions of your use case.

Updated on: 07-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements