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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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:
- Functions in Python
- Data Structures in Python
- OOPs in Python
- Regular expressions
- Python String Concatenation
Refer to our Guided Path to upskill yourself in DSA, Competitive Programming, JavaScript, System 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 problems, interview experiences, and interview bundles for placement preparations.