Using split method to generate sub-strings from strings in Python [with Examples]

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 :

A= “washington”

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:

Simple code explaining string handling in python
Simple code explaining string handling in python

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:

Looping through python strings
Looping through python strings

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:

Python String Split: Using split method in python to extract words from a phrase
Python String Split: Using split method in python to extract words from a phrase

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 ="john300@gmail.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.

Further reading:

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

Can you split a string in Python?

You can split a string in Python by using the split method.

What is the syntax of the string split method in Python ?

The syntax for string split method is string.split(SEPARATOR, MAXSPLIT)

Summary
Using split method to generate sub-strings from strings in Python [with Examples]
Article Name
Using split method to generate sub-strings from strings in Python [with Examples]
Description
Understanding how to use split function is important for understanding string handling in python. Learn the details of using split function with examples.
Author
Publisher Name
Digital Marketing Chef
Publisher Logo

2 thoughts on “Using split method to generate sub-strings from strings in Python [with Examples]”

  1. Hi there very nice site!! Guy .. Beautiful .. Amazing .. I will bookmark
    your blog and take the feeds also? I am satisfied to search out so many useful information right here in the
    put up, we want develop more strategies on this regard,
    thanks for sharing. . . . . .

Leave a Reply

Your email address will not be published. Required fields are marked *