![]() |
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 PythonIn Python, you can split a string into smaller parts using the Syntax :
Parameters:
Return Value: The str.split() MethodThis 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
Output : ['Hello', 'Welcome', 'to', 'GeeksforGeeks']
str.rsplit() MethodThis 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
Output : ['Hello,to,World', 'Python']
str.splitlines() MethodThis 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
Output : ['GFG 1', 'GFG 2', 'GFG 3']
str.partition() MethodThis 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
Output : Mango
Orange,apple
re.split() MethodThe 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
Output : ['Mango,Orange', 'Banana']
Using List ComprehensionYou 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
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 FloatIf the string contains numeric data. you can parse it to an integer or float data type using the Python
Output : 52
4.17
ConclusionBy 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. |
Reffered: https://www.geeksforgeeks.org
Python |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |