Sometimes, we come across a situation where the data of our interest is distributed across the whole string in chunks. To extract these chunks from the whole string, string parsing is used. String parsing is the process of dividing the string into tokens using delimiters to extract the desired information. This tutorial is about How to parse a string in python. We will learn how to parse the data strings into a list to extract our desired information using different methods and functions. If you want to learn more about Python Programming, visit Python Programming Tutorials.
In this article, we will cover the following topics:
- Parse String into integer or float variable
- Parse String data into a List
Parse String INTo integer or Float variable
The int() function can be used to parse the input string into an integer variable. Similarly, to parse the string data into a floating variable, we can use float() function. For example
#Program to parse a string into integer variable
#Take a string as an input from the user
var = input("Enter any number: ")
#print the datatype of the input string
print("Before parsing: ")
print(type(var))
# Parse the string variable to integer
new_var=int(var)
#display the datatype of parsed variable
print("After parsing: ")
print(type(new_var))
# Parse the string variable to float variable
new_var=float(var)
#display the datatype of parsed variable
print("After parsing: ")
print(type(new_var))
Enter any number: 3246
Before parsing:
<class 'str'>
After parsing:
<class 'int'>
After parsing:
<class 'float'>
The type() function shows the datatype of the variable.
Parse String data Into a List
The str.split() function is used to parse a string into list. A delimiter or separator is passed as an input parameter to the str.split() function, which splits the input string using the delimiter and returns a list of substrings. Suppose you have a string ‘Pencil(10Rs), Rubber(5Rs), Ruler(8Rs),Sharpener(5Rs)’. Now, you are asked to display the price of Rubber. The code snippet below demonstrates how to use the str.split() function to convert a string representation of a list into a real list. Suppose you have a str
#initialize a string
input_string = 'Pencil(10Rs),Rubber(5Rs),Ruler(8Rs),Sharpener(5Rs)'
#create a new lists by parsing the string using "," delimiter
new_string=input_string.split(",")
print("After string parsing: ",new_string)
print("The price of Rubber is: ",new_string[1])
After string parsing: ['Pencil(10Rs)', 'Rubber(5Rs)', 'Ruler(8Rs)', 'Sharpener(5Rs)']
The price of Rubber is: Rubber(5Rs)
Using the input_string.split(“,”) function, we have divided the string of items into a list consisting of items along with their prices. Now, you can easily find the price of any item. You can also define the maximum number of splits in a string. Call the same split() function and now pass two arguments to this function i.e., str.split(delimiter, maximum_splits). Here, maximum_splits specifies the number of splits that you want. Consider the above example. Suppose you want only two splits.
#initialize a string
input_string = 'Pencil(10Rs)/Rubber(5Rs)/Ruler(8Rs)/Sharpener(5Rs)'
#create a new lists by parsing the string using "/" delimiter
new_string=input_string.split("/", 2)
print("After string parsing: ",new_string)
After string parsing: ['Pencil(10Rs)', 'Rubber(5Rs)', 'Ruler(8Rs)/Sharpener(5Rs)']
It can be seen from the output that the string is divided into two parts using first two delimiters while the remaining string remains the same.