Python in general is a fantastic language for all kinds of string handling. In this tutorial we will learn how to do python string handling with special emphasis on split function.
Basics of Python strings
Python strings essentially behave like Python lists. If you understand lists well you don’t need to worry too much about strings. Let’s understand why:
Consider the following strings :
B= “How are you doing?”
Python stores these strings like lists & thus all the typical list operators are applicable for strings. Here’s how it looks like:
A = [‘w’, ‘a’, ‘s’, ‘h’ , ‘i’, ‘n’ ,’g’, ‘t’ , ‘o’ ,’n’]
B=[‘H’, ‘o’, ‘w’, ‘ ‘ , ‘a’, ‘r’, ‘e’, ‘ ‘,y’, ‘o’, ‘u’, ‘ ‘, ‘d’, ‘o’, ‘i’, ‘n’, ‘g’, ‘?’]
The letters and special characters in the strings behave like members of a list.
Check the following code bit to understand how string operations work in python. The code prints the following:
- The strings
- The length of the strings
- The first 3 characters in a string
- The last 3 characters in a string
A='washington' B='How are you doing?' print(A) print(B) #Length of strings: print(len(A)) print(len(B)) #Capturing first 3 characters in a string print(A[:3]) print(B[:3]) #Capturing last 3 characters in a string print(A[-3:]) print(B[-3:])
The output for the code is as follows:
Just like lists, you can use the “+” operator to concatenate 2 strings:
C = A+" " +B print(C)
The output will be:
“washington How are you doing?”
You can also loop through a string using a for loop:
for item in A: print(item)
The output looks like:
Now let’s understand how to do some advanced string handling in python.
string.split() method in Python
Python strings just like lists are essentially objects. Split method allows us to split a string as per a given separator.
The syntax for splitting a string in Python is as follows:
string.split( separator , maxsplit)
Separator: Here we pass the delimiter value to be used for separating a string. If no value is passed then whitespace is considered as the separator.
maxsplit: This specifies the maximum number sub-strings that we want to generate. If no value is passed for the maxsplit argument, the maximum possible sub-strings are created.
Example 1: Using string.split() to extract words from a sentence
Can you extract words from a sentence using Python ? Split function allows you to that easily.
What a split function does is split a Python string to list of substrings using a specific separator.
Consider the following string:
string = “Jack and Jill”
To extract the words from the above phrase, we would need to first identify the separator. In any English sentence or phrase, two consecutive words are separated by a blank space.
Python understands blank space as : ” “
So, when we use the following function:
B = string.split(” “)
We get :
B = [“Jack”, “and” , “Jill]
We can then simply loop through B to get the words. Check the full code below:
A = "Jack and Jill" B = A.split(" ") print(B) for item in B: print(item)
The output looks like:
Example 2: Split a string by comma in Python:
Just like above, we can use any separator to split strings to sub-strings accordingly.
Consider the following code:
string = "Tom,John,Mark,Gary" print(string.split(","))
The output is a neat list : [‘Tom’, ‘John’, ‘Mark’, ‘Gary’]
Split string by regex separator in Python
Using regex we can split a string by multiple separators. For example, we want to split an email address to extract the username , domain & extension in a list. We can do it using the following code. Note that you need to import “re” module (regex) and the syntax is slightly different :
import re email ="email@example.com" output =re.split("@|.",email) print(output)
The output would be : [‘john300’, ‘gmail’, ‘com’]
Isn’t it a great way to build a database from email IDs ?
You could check this site to learn more about regex.
I hope you enjoyed reading this basic tutorial on python strings. If you are keen to learn python , you could also check out my other articles on Python.
FAQs : Python String Split
You can split a string in Python by using the split method.
The syntax for string split method is string.split(SEPARATOR, MAXSPLIT)