Jan 23, 2023 2 min read

Step-by-Step Tutorial to Split a String in Python

In this tutorial, you will see how to split a string in Python, which is a common task in many programming languages.

Split a String in Python
Table of Contents

Introduction

Splitting a string is a common task in many programming languages, and Python is no exception. In Python, it can be done in multiple ways, depending on the desired outcome.

The simplest way to split a string is to use the built-in split method. This method takes an optional argument, which specifies the character or characters to use for splitting the string. If no argument is provided, the default behavior is to split the string on whitespace. Other methods of splitting strings, such as using regular expressions, are also available.

In this tutorial, you will see how to split a string in Python.

.split() Method

Strings are modeled as immutable str objects in Python. There are several string methods available in the str class that let you work with the string.

The list of substrings returned by the .split() method is separated by a delimiter. Its syntax is as follows:

str.split(delim=None, maxsplit=-1)

Instead of a regular expression, the delimiter can be a single character or a string of characters.

In the following example, the string s is split using the comma (,) as a delimiter:

s = 'Sansa,Tyrion,Jon'
s.split(',')

The output will display a list of strings:

['Sansa', 'Tyrion', 'Jon']
String literals are generally enclosed by single quotes, however double quotes can also be used.

You can also use a sequence of characters as a delimiter:

s = 'Sansa::Tyrion::Jon'
s.split('::')
Output

['Sansa', 'Tyrion', 'Jon']

The number of splits is limited when maxsplit is specified. There is no limit to the number of splits if either the value is undefined or -1.

s = 'Sansa;Tyrion;Jon'
s.split(';', 1)

There will be no more than maxsplit+1 elements in the result list:

Output

['Sansa', 'Tyrion;Jon']

The string will be divided using whitespace as a delimiter if the delim is not supplied or is Null. All subsequent whitespace are treated as a single separator. Additionally, the result will not contain any empty strings if the string has a leading and trailing whitespace.

Let us look at the following example to better illustrate this:

' John  Sam Allen   James Brian '.split()
Output

['John', 'Sam', 'Allen', 'James', 'Brian']
' John  Sam Allen   James Brian '.split(' ')
Output

['', 'John', '', 'Sam', 'Allen', '', '', 'James', 'Brian', '']

There are no empty strings in the returning list when there is no delimiter. If the delimiter is set to an empty space ' ', the result will contain empty strings due to leading, trailing, and consecutive whitespace.

Conclusion

Hope this detailed tutorial, helped you understand how to divide strings in Python.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.