Split and Parse a string in Python
Last Updated :
27 Sep, 2023
In Python, working with strings is a fundamental aspect of programming. Strings are sequences of characters and often contain structured data that needs to be processed or analyzed. The common operations performed on strings are splitting and parsing.
Splitting a String in Python
In Python, you can split a string into smaller parts using the split()
method and The split()
method takes a delimiter as an argument and breaks the string at each occurrence of the delimiter returning a list of substrings.
Syntax :
string.split(delimiter)
Parameters:
delimiter
: The character or substring at which the string will be split. It is an optional parameter.
Return Value:
The split()
method returns a list of substrings obtained by breaking the original string at occurrences of delimiter.
str.split() Method
This method splits a string into a list of the substrings based on a specified delimiter. The string “Hello, Welcome to GeeksforGeeks” using the comma (‘,’) as a delimiter. The split() function separates the string into a list of substrings using delimiter resulting in the list A containing [“Hello” “Welcome” “to” “GeeksforGeeks”].
Example :
Python
string = "Hello,Welcome,to,GeeksforGeeks"
A = string.split( ',' )
print (A)
|
Output :
['Hello', 'Welcome', 'to', 'GeeksforGeeks']
str.rsplit() Method
This is similar to str.split(), but it splits from right side of the string. The given code uses the rsplit() function to split the string S into the two parts based on the last occurrence of the comma (‘,’). The result is a list containing the parts before and after the split. A will be [‘Hello to World’, ‘Python’] where the last comma-separated portion “Python” is the separated from rest of the string “Hello to World”.
Example :
Python
S = "Hello,to,World,Python"
A = S.rsplit( ',' , 1 )
print (A)
|
Output :
['Hello,to,World', 'Python']
str.splitlines() Method
This method splits a string at line breaks and returns a list of the lines. The code defines a string M with the three lines of text separated by newline characters. It then uses the splitlines() method to split the string into a list called line and where each element represents a line from the original string. Finally, the code prints the contents of line list.
Example :
Python
M = "GFG 1\nGFG 2\nGFG 3"
line = M.splitlines()
print (line)
|
Output :
['GFG 1', 'GFG 2', 'GFG 3']
str.partition() Method
This method splits a string into three parts based on first occurrence of a separator. The code uses the partition() method to split the string Text at the first occurrence of separator. It assigns the part before the separator to variable before, the separator itself to variable sep, and the part after the separator to variable after.
Example :
Python
Text = "Mango,Orange,apple"
before, sep, after = Text.partition( ',' )
print (before)
print (after)
|
Output :
Mango
Orange,apple
re.split() Method
The re module provides more powerful string splitting using the regular expressions. The code uses the re.split() function from re module in Python to split the Text string based on presence of digits. It creates a list P where elements are substrings of the Text separated by digits. The output would be [‘Mango,Orange’, ‘Banana’] showing that the string has been split at digit ‘1’.
Example :
Python
import re
Text = "Mango,Orange1Banana"
P = re.split(r '\d' , Text)
print (P)
|
Output :
['Mango,Orange', 'Banana']
Using List Comprehension
You can use list comprehension to split a string and filter out empty strings. The code splits the string Text by the commas using the split(‘,’) then uses a list comprehension to create a list P containing the non-empty parts after removing any leading or trailing whitespace. The output is the list P containing [‘Mango’, ‘Orange’, ‘Banana’]. This code effectively removes empty parts from the split result and trims any extra spaces.
Example :
Python
Text = "Mango,Orange,Banana"
P = [part for part in Text.split( ',' ) if part.strip()]
print (P)
|
Output :
['Mango', 'Orange', 'Banana']
In Python, The parsing a string refers to extracting meaningful information from the given string and This process involves converting the string into its desired data type or extracting specific data patterns from string.
Converting to Integer or Float
If the string contains numeric data. you can parse it to an integer or float data type using the int()
and float()
functions, respectively. The given code converts a string “52” to an integer and prints the result (52). Similarly, it converts the string “4.17” to a floating-point number and prints the result (4.17). This demonstrates the use of the int() and float() functions to convert strings to the corresponding numeric data types in Python.
Python
number_str = "52"
number_int = int (number_str)
print (number_int)
decimal_str = "4.17"
decimal_float = float (decimal_str)
print (decimal_float)
|
Output :
52
4.17
Conclusion
By using these methods mentioned above, you will be able to Split and parse a string. We will also explore How we can convert a interger to float and by using split() method how we can break a string into smaller parts based on a delimiter.
Share your thoughts in the comments
Please Login to comment...