How to use Split in Python
The split() method in Python returns a list of the words in the string/line , separated by the delimiter string. This method will return one or more new strings. All substrings are returned in the list datatype. Syntax
string.split(separator, max)
Parameter | Description |
---|---|
separator | The is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator. |
maxsplit | It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then there is no limit. |
return | The split() breaks the string at the separator and returns a list of strings. |
Splitting String by space
The split() method in Python without an argument splits on whitespace. example
str = "This is a test"
print(str.split())
output
['This', 'is', 'a', 'test']
Splitting on first occurrence
In the following example, it will Split by first 2 whitespace only.
example
str = "This is a test"
print(str.split(" ",2))
output
['This', 'is', 'a test']
Splitting lines from a text file in Python
The following Python program reading a text file and splitting it into single words in python example
with open("my_file.txt", "r") as my_file:
for line in my_file:
str = line.split()
print(str)
Splitting String by newline(\n)
str = "This \nis \na \ntest"
print(str)
print("\nAfter Split\n")
print(str.split())
output
This
is
a
test
After Split
['This', 'is', 'a', 'test']
Splitting String by tab(\t)
str = "This \tis \ta \ttest"
print(str)
print("\nAfter Split\n")
print(str.split())
output
This is a test
After Split
['This', 'is', 'a', 'test']
Splitting String by comma(,)
str = "This,is,a,test"
print(str.split(","))
output
['This', 'is', 'a', 'test']
Split string with multiple delimiters
In this case Python uses Regular Expression. example
import re
str = "This,isa;test"
print(re.split(",;",str))
output
['This', 'is', 'a', 'test']
Split a string into a list
The following Python program split a string to a List. example
str = "This is a test"
lst = str.split()
for st in lst:
print(st)
output
This
is
a
test
maxsplit parameter
Split the string into a list with max 2 items
numbers = "one two three four five six"
result = numbers.split(" ",2)
print (result)
output
['one', 'two', 'three four five six']
In the above program maxsplit is 2, the first two string are split and rest of them are in a same string.
Split a string into array of characters
characters = "abcdef"
result = list(characters)
print (result)
output
['a', 'b', 'c', 'd', 'e', 'f']
Python split() using substring
Extact a string after a specific substring.

In the above example, you can see the split() function return next part of a string using a specific substring.

Here, you can see the split() function return the previous part of the string using a specific substring.
Looking for a Python job ?
Chances are you will need to prove that you know how to work with Python. These Python Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Python Programming . Here are the top objective type sample Python Interview questions and their answers are given just below to them. These sample questions are framed by our experts team who trains for Python training to give you an idea of type of questions which may be asked in interview. Go to... Python Interview Questions
Related Topics
- How to open and close a file in Python
- How to write files in python
- How to read a file properly in Python
- How to read a file line by line in Python
- Directory Operations Using Python
- How to check that a file or directory exists with Python
- File Access Mode in Python
- Difference between read, readline and readlines | python
- Python file positions | seek() and tell()
- How to Copy a File using Python