Table of contents
1.
Introduction
2.
What is Python split String?
3.
Syntax 
4.
Parameters of Python String split() Method
5.
Python String split() Method Examples
5.1.
Example 1: Python split string with a specified separator parameter
5.2.
Python
5.3.
Example 2: Python split string with separator as well as maxsplit parameter
5.4.
Python
5.5.
Example 3: Python split string with separator as a regular expression and maxsplit parameter
5.6.
Python
6.
How split() works in Python?
6.1.
Python
7.
Multiple Uses of the split() Function
7.1.
Splitting a String by Space Delimiter
7.2.
Python
7.3.
Split String at First Character Occurrence
7.4.
Python
7.5.
Split File into List
7.6.
Splitting the String Using the Newline Character as the Delimiter
7.7.
Python
7.8.
Splitting the String Using the Tab Delimiter
7.9.
Python
7.10.
Splitting the String Using the Comma Delimiter
7.11.
Python
7.12.
Splitting the String Using Multiple Delimiters
7.13.
Python
7.14.
Splitting the String to Form a List
7.15.
Python
7.16.
Splitting the String Using the Hash Delimiter
7.17.
Python
7.18.
Splitting the String by Specifying the Maxsplit Parameter
7.19.
Python
7.20.
Splitting the String into an Array of Characters
7.21.
Python
7.22.
Splitting the String Using One of the Substrings from the Provided String as the Delimiter
7.23.
Python
8.
Frequently Asked Questions
8.1.
What does split() do in Python?
8.2.
What is input() split() in Python?
8.3.
How to split the list in Python?
8.4.
How does split () work in Python?
8.5.
How to split 1 string into 2 Python?
9.
Conclusion 
Last Updated: Aug 3, 2024
Medium

Python String split() Method

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Python, the split string method is commonly used to divide strings. In this article, we will discuss Python's split() method in detail. We will understand Python split string's syntax, parameters, and return type. Further, we will see its implementation in a few examples.

Python String split() Method

Also read about strings in Python.

What is Python split String?

The Python split string method is a built-in function in the Python programming language to split a specific string into a list of substrings based on a delimiter. 

It makes string manipulation easy and efficient. The split() method can be used in Python to retrieve useful data from any text and use it for data analysis.

Syntax 

The syntax of Python split string is as follows:

str.split(separator, maxsplit)

 

Also, check out our article on Python syntax.

Parameters of Python String split() Method

The Python split spring accepts two arguments as its parameters:

  • separator: The separator parameter states the character, a sequence of characters, or any regular expression at which the split should occur. It is an optional parameter; if not specified, whitespace is taken as a default value.
     
  • maxsplit: maxsplit refers to the number of splits to be made. It is an optional parameter. If the maxsplit value is not specified with the split() method, it is by default set to -1 with no limit to the number of splits made. If the maxsplit value is a positive integer, a list with n elements is returned. 

Python String split() Method Examples

Now let us understand the implementation of the split() function in Python with a few examples.

Example 1: Python split string with a specified separator parameter

Code:

  • Python

Python

Str = "The Quick brown cat jumped over the lazy dog"
words = Str.split(" ")
print(words)
You can also try this code with Online Python Compiler
Run Code

Output:

['The,' 'Quick,' 'brown,' 'cat,' 'jumped,' 'over,' 'the,' 'lazy,' 'dog']

 

Explanation: The split() method splits the input string based on the space character in the above code. Thus a list of substrings with the words of the string is returned as an output.

Example 2: Python split string with separator as well as maxsplit parameter

Code:

  • Python

Python

Str = "Hello Ninja, Welcome to Coding Ninjas Studio, Keep Learning, All the best"
words = Str.split(",", 2)
print(words)
You can also try this code with Online Python Compiler
Run Code

Output:

['Hello Ninja,'' Welcome to Coding Ninjas Studio,'' Keep Learning, All the best']

 

Explanation: Here, the split() method splits the string str into three substrings based on the comma character and stops after the second split because the value of the maxsplit parameter is assigned to 2.

Example 3: Python split string with separator as a regular expression and maxsplit parameter

Code:

  • Python

Python

import re
str = "Coding Ninjas is the best"
ans = re.split("\W+", str)
print (ans)
You can also try this code with Online Python Compiler
Run Code

Output:

['Coding,' 'Ninjas,' 'is, ''the,' 'best']

 

Explanation: In this example, we have used a regular expression to split the string str.

How split() works in Python?

In Python, the split() method is a built-in string method that is used to split a string into a list of substrings based on a specified delimiter. The split() method takes an optional parameter called sep (separator) that defines the delimiter character or substring.

The split() method takes the sentence and splits it into words, using spaces as separators. It returns a list of words:

For example:

Code:

  • Python

Python

sentence = “Welcome to Coding Ninjas”
words = sentence.split()
print(words)
You can also try this code with Online Python Compiler
Run Code

Output:

[ ‘Welcome’ , ‘to’ , ‘Coding’ , ‘Ninjas ']

Now, we have each word from the sentence separately in the words list. The split() method helps us break a sentence into individual words by finding spaces between them. 

Multiple Uses of the split() Function

The split() function in Python is a versatile method used to divide a string into a list of substrings based on a specified delimiter. Here are various ways to use the split() function:

Splitting a String by Space Delimiter

The default behavior of split() is to split a string by whitespace.

Example:

  • Python

Python

text = "Python is awesome"
result = text.split()
print(result) # Output: ['Python', 'is', 'awesome']
You can also try this code with Online Python Compiler
Run Code

 

Output:

['Python', 'is', 'awesome']

Split String at First Character Occurrence

To split a string at the first occurrence of a character, you can use split() with a specific delimiter and the maxsplit parameter set to 1.

Example:

  • Python

Python

text = "apple:banana:cherry"
result = text.split(':', 1)
print(result) # Output: ['apple', 'banana:cherry']
You can also try this code with Online Python Compiler
Run Code

 

Output:

['apple', 'banana:cherry']

Split File into List

To split the contents of a file into a list, first read the file content as a string, then use split().

Example:

# Assuming 'file.txt' contains:
# line1
# line2
# line3

with open('file.txt', 'r') as file:
    content = file.read()
    result = content.split()
    print(result)  # Output: ['line1', 'line2', 'line3']

Output:

['line1', 'line2', 'line3']

Splitting the String Using the Newline Character as the Delimiter

To split a string by newline characters, use split('\n').

Example:

  • Python

Python

text = "line1\nline2\nline3"
result = text.split('\n')
print(result) # Output: ['line1', 'line2', 'line3']
You can also try this code with Online Python Compiler
Run Code

 

Output:

['line1', 'line2', 'line3']

Splitting the String Using the Tab Delimiter

To split a string by tabs, use split('\t').

Example:

  • Python

Python

text = "column1\tcolumn2\tcolumn3"
result = text.split('\t')
print(result) # Output: ['column1', 'column2', 'column3']
You can also try this code with Online Python Compiler
Run Code

 

Output:

['column1', 'column2', 'column3']

Splitting the String Using the Comma Delimiter

To split a string by commas, use split(',').

Example:

  • Python

Python

text = "apple,banana,cherry"
result = text.split(',')
print(result) # Output: ['apple', 'banana', 'cherry']
You can also try this code with Online Python Compiler
Run Code

 

Output:

['apple', 'banana', 'cherry']

Splitting the String Using Multiple Delimiters

To split a string using multiple delimiters, you'll need to use regular expressions with the re module, as split() only accepts a single delimiter.

Example:

  • Python

Python

import re

text = "apple;banana,cherry orange"
result = re.split('[,; ]+', text)
print(result) # Output: ['apple', 'banana', 'cherry', 'orange']
You can also try this code with Online Python Compiler
Run Code

 

Output:

['apple', 'banana', 'cherry', 'orange']

Splitting the String to Form a List

To split a string into a list of substrings based on a specified delimiter.

Example:

  • Python

Python

text = "one,two,three,four"
result = text.split(',')
print(result) # Output: ['one', 'two', 'three', 'four']
You can also try this code with Online Python Compiler
Run Code

 

Output:

['one', 'two', 'three', 'four']

Splitting the String Using the Hash Delimiter

To split a string by the hash # delimiter.

Example:

  • Python

Python

text = "section1#section2#section3"
result = text.split('#')
print(result) # Output: ['section1', 'section2', 'section3']
You can also try this code with Online Python Compiler
Run Code

 

Output:

['section1', 'section2', 'section3']

Splitting the String by Specifying the Maxsplit Parameter

To limit the number of splits, use the maxsplit parameter.

Example:

  • Python

Python

text = "apple orange banana grape"
result = text.split(' ', 2)
print(result) # Output: ['apple', 'orange', 'banana grape']
You can also try this code with Online Python Compiler
Run Code

 

Output:

['apple', 'orange', 'banana grape']

Splitting the String into an Array of Characters

To split a string into individual characters, use list().

Example:

  • Python

Python

text = "hello"
result = list(text)
print(result) # Output: ['h', 'e', 'l', 'l', 'o']
You can also try this code with Online Python Compiler
Run Code

 

Output:

['h', 'e', 'l', 'l', 'o']

Splitting the String Using One of the Substrings from the Provided String as the Delimiter

To split a string using a substring as a delimiter, simply use split() with that substring.

Example:

  • Python

Python

text = "start:middle:end"
delimiter = "middle"
result = text.split(delimiter)
print(result) # Output: ['start:', ':end']
You can also try this code with Online Python Compiler
Run Code

 

Output:

['start:', ':end']

Frequently Asked Questions

What does split() do in Python?

The split() function divides a string into a list of substrings based on a specified delimiter or whitespace by default.

What is input() split() in Python?

Using input().split() reads a user input as a string and splits it into a list of substrings based on whitespace.

How to split the list in Python?

To split a list, use slicing: list[start:end] or methods like numpy.array_split() for more advanced splitting.

How does split () work in Python?

In Python, the 'split()' method divides a string into smaller parts using a specified delimiter. It searches for the delimiter within the string, breaks it apart at those points, and then provides a list containing the resulting substrings.

How to split 1 string into 2 Python?

We can split a string into two parts using the 'split()' method in Python. This method will divide the string into two parts where it finds space or a specified character.

Conclusion 

This article has covered everything you need to know about the Python split string. It is a very important concept in Python.

We hope this blog has helped you understand Python's split() function. Keep learning! We recommend you read some of our other articles on Python: 

  1. Functions in Python
  2. Data Structures in Python
  3. OOPs in Python
  4. Regular expressions
  5. Python String Concatenation

 

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. You must look at the problemsinterview experiences, and interview bundles for placement preparations.

Live masterclass