How to Parse a String in Python?

In this tutorial, I will explain how to parse a string in Python. As a data scientist working for a marketing analytics firm in New York, I recently worked on a project analyzing customer reviews to identify trends and insights. I needed to parse these strings and separate the data into structured fields to extract meaningful insights. After exploring multiple approaches, I identified efficient parsing techniques.

String Parsing in Python

Python String parsing involves breaking down a string into smaller components or extracting specific parts of a string based on certain criteria. This is particularly useful when you have a string that follows a specific format or contains delimited data. By parsing the string, you can extract the desired information and work with it separately.

For example, let’s say you have a string that represents a person’s details:

person_info = "name:John,age:25,city:New York"

In this case, you might want to parse the string to extract the person’s name, age, and city. Python provides various methods to accomplish this task.

Read How to Add Line Breaks in Python Strings?

Parse a String in Python

Python provides several ways to parse a string in Python. Let us see some important methods.

1. Use the split() Method

One of the most common ways to parse a string in Python is by using the split() method. The split() method allows you to split a string into a list of substrings based on a specified delimiter.

Let’s take an example:

text = "Hello, my name is Sarah. I live in Los Angeles."
sentences = text.split(". ")
print(sentences)

Output:

['Hello, my name is Sarah', 'I live in Los Angeles.']

I have executed the above example code and added the screenshot below.

Parse a String in Python

In this example, we split the text string into sentences using the delimiter . (dot followed by a space). The resulting sentences list contains two elements, each representing a sentence from the original string.

You can also split a string based on a specific character or substring. For instance, let’s parse the person_info string from the previous example:

person_info = "name:John,age:25,city:New York"
details = person_info.split(",")
print(details)

Output:

['name:John', 'age:25', 'city:New York']

I have executed the above example code and added the screenshot below.

How to Parse a String in Python

Here, we split the person_info string using the comma (,) as the delimiter. The resulting details list contains three elements, each representing a key-value pair.

To further parse the key-value pairs, you can use the split() method again:

for detail in details:
    key, value = detail.split(":")
    print(f"{key}: {value}")

Output:

name: John
age: 25
city: New York

I have executed the above example code and added the screenshot below.

Parse a String in Python partition()

In this code, we iterate over each element of the details list and split it further using the colon (:) as the delimiter. We then print the key and value separately.

Read How to Remove Commas from a String in Python?

2. Use Regular Expressions

Regular expressions (regex) provide a powerful way to parse strings based on patterns. Python’s re module allows you to work with regular expressions effectively.

Let’s consider an example where we want to extract email addresses from a string:

import re

text = "Contact us at info@example.com or support@example.org"
pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"

emails = re.findall(pattern, text)
print(emails)

Output:

['info@example.com', 'support@example.org']

In this example, we define a regular expression pattern that matches email addresses. The re.findall() function finds all occurrences of the pattern in the text string and returns them as a list.

Read How to Continue Long Strings on the Next Line in Python?

3. Use the Partition() Method

Another useful method for parsing strings in Python is the partition() method. It allows you to split a string into three parts based on a specified separator: the part before the separator, the separator itself, and the part after the separator.

Let’s take an example:

text = "Date of Birth: 1990-05-15"
dob = text.partition(": ")[2]
print(dob)

Output:

1990-05-15

In this example, we use the partition() method to split the text string based on the substring :. The resulting tuple contains three elements: the part before the separator, the separator itself, and the part after the separator. We access the third element of the tuple ([2]) to get the date of birth.

Read How to Count Words in a String Using Python?

Conclusion

In this tutorial, I have explained how to parse a string in Python. I covered different approaches to parsing strings in Python, including using the split() method, regular expressions , and the partition() method.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.