How to split and parse a string in Python?

In this python tutorial, we will see how to split and parse a string in Python.

Table Of Contents

Split and parse a string using split()

In Python, string class provides a function split(), to break the string into a list of substrings. It splits the string based on few optional arguments i.e.

  • sep : A delimiter. By default, the delimiter is a whitespace.
  • maxsplit : The maximum number of splits it will do. Default value for maxsplit is -1, which means no limit.

We can use this to split a string into list of words. Let’s see an examples,

Example 1: Split a string by using space as the delimiter string

Here we will consider the string – “Welcome to thisPointer” and split it into words by using a whitespace as a delimeter.

Advertisements
strValue = "Welcome to thisPointer"

print(strValue)

# Split the string
listOfWords = strValue.split()

print(listOfWords)

Output:

Welcome to thisPointer
['Welcome', 'to', 'thisPointer']

The above string is split into three words.

Read More  Add two Lists element wise in Python

Example 2: Split a string by using comma as the delimiter string

here, we will provide a custom separator to the split() function i.e. a comma. It will split the string based on it. Let’s see an example,

strValue = "Welcome,to,this,Pointer"

print(strValue)

# Split the string by using comma as separator
listOfWords = strValue.split(sep=',')

print(listOfWords)

Output :

Welcome,to,this,Pointer
['Welcome', 'to', 'this', 'Pointer']

Example 3: Split a string by a character in Python

We can pass any character as the separator in the split() function. Let’s see an example, where we will pass character ‘o’ as the separator.

Read More  Python : How to unpack list, tuple or dictionary to Function arguments using * & **
strValue = "Welcome to this Pointer"

print(strValue)

# Split the string by using letter 'o' as delimeter
listOfWords = strValue.split(sep='o')

print(listOfWords)

Output :

Welcome to this Pointer
['Welc', 'me t', ' this P', 'inter']

Example 3: Split a string into two substrings in Python

By default, the split() function splits a string into all possible substrings. But what if we are interested in only few substrings? For example, by default the split() function will break the string “Welcome to this Pointer” into four substrings. If we want it to break the string into two substrings only, then we can pass the value of maxsplit as 1 in the split() function. Let’s see an example,

strValue = "Welcome to this Pointer"

print(strValue)

# Split the string into two substrings
listOfSubstrings = strValue.split(maxsplit=1)

print(listOfSubstrings)

Output :

Read More  Python: Get difference between two datetimes in minutes
Welcome to this Pointer
['Welcome', 'to this Pointer']

It broke the string into a list of substrings by using space as a delimeter, and returned only two substrings.

Summary

We learned how to split and parse a string using split() function in Python.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top