- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- How to extract certain substring from a string using Java?
- How can we extract a substring from a string in MySQL?
- How to extract numbers from a string in Python?
- How to extract date from a string in Python?
- How to extract numbers from a string using Python?
- Python – How to Extract all the digits from a String
- How can we get substring from a string in Python?
- Extract decimal numbers from a string in Python
- How to extract data from a string with Python Regular Expressions?
- How to find a substring from a string in C#?
- How to extract substring from a sting starting at a particular position in MySQL?
- How to check if string or a substring of string starts with substring in Python?
- How to extract characters from a string in R?
- In how many ways can we find a substring inside a string in javascript?
- Python Regex to extract maximum numeric value from a string
