Page Contents
In this tutorial we will learn how to split a string in Python . Python string provides 5 built-in methods to split a string in Python . They are as follows –
- Python string split() method
- Python string rsplit() method
- Python string partition() method
- Python string rpartition() method
- Python string splitlines() method
Let us have a look about all this methods with examples –
Python String split() method
It returns a list of words in the string separated by a given delimiter .
Syntax of split() method
string.split([sep[,maxsplit]])
Parameters of split() method
- sep = Optional . It is a string based on which the given string will be separated . Default value is 1 whitespace (
" "
) - maxsplit = Optional . It is a integer number specifying maximum no of split . Default value is -1 which splits all the item .
Return type of split() method
It returns a list .
Examples of Python String split() method
Example 1: Split a string by space Python
# declare a string
mystring="Python is a popular programming language"
# split the string based on whitespace
mylist=mystring.split()
print(mylist)
['Python', 'is', 'a', 'popular', 'programming', 'language']
- In the above code first we have declared a string named
mystring
as"Python is a popular programming language"
- Then we have split the string using
split()
method . - As we have not given any parameter in the split() method , so we can see from the output that the string is split base on whitespace .
Example 2: Take a list as input from user in Python
# taking a string input from user
mystring=input("Enter the numbers separated by comma :")
# split the string based on comma
mylist=mystring.split(",")
print(mylist)
Enter the numbers separated by comma :1,2,3,4,5 ['1', '2', '3', '4', '5']
- In the above code we have taken multiple numbers separated by comma (
","
) as input from user and stored it inmystring
variable . - Then the string is split by split() method with
","
parameter . - from the output we can see that the string is split based on comma (
","
)
Example 3:
# declare a string
mystring="Python is a popular programming language"
# split the string based on whitespace
mylist=mystring.split(" ",4)
print(mylist)
['Python', 'is', 'a', 'popular', 'programming language']
In the above code as we have given 4
as maxsplit
parameter so the returned list contains first 4 words of the string separated by whitespace and the rest part of the string .
Python String rsplit() method
It returns a list of words in the string separated by a given delimiter (starting from right side).
Syntax of rsplit() method
string.rsplit([sep[,maxsplit]])
Parameters of rsplit() method
- sep = Optional . It is a string based on which the given string will be separated . Default value is 1 whitespace (
" "
) - maxsplit = Optional . It is a integer number specifying maximum no of split . Default value is -1 which splits all the item .
Return type of rsplit() method
It returns a list .
Examples of Python String rsplit() method
Example 1: Split a string based on whitespaces using rsplit method
# declare a string
mystring="Python is a popular programming language"
# split the string based on whitespace
mylist=mystring.rsplit()
print(mylist)
['Python', 'is', 'a', 'popular', 'programming', 'language']
Example 2: Take a list as input from user in Python using rsplit method
# taking a string input from user
mystring=input("Enter the numbers separated by comma :")
# split the string based on comma
mylist=mystring.rsplit(",")
print(mylist)
Enter the numbers separated by comma :1,2,3,4,5,6 ['1', '2', '3', '4', '5', '6']
Example 3:
# declare a string
mystring="Python is a popular programming language"
# split the string based on whitespace
mylist=mystring.rsplit(" ",4)
print(mylist)
['Python is', 'a', 'popular', 'programming', 'language']
In the above code we can see that as rsplit()
starts splitting from right side so the returned list contains last 4 words separated by whitespace from the string and the rest part of the string .
Python String partition() method
It returns a tuple containing the left part of the string after splitting by the specified separator on its first occurrence , specified separator and the right part of the string .
Syntax of partition() method
string.partition(sep)
Parameters of partition() method
- sep = It is a string based on which the given string will be separated . If the separator is not found then the returned tuple will contain the entire string with two empty strings .
Return type of partition() method
It returns a tuple .
Examples of Python String partition() method
Example 1:
# declare a string
mystring="Python is a popular programming language"
# split the string based on first "a"
mytuple=mystring.partition("a")
print(mytuple)
('Python is ', 'a', ' popular programming language')
- In the above code first we have declared a string named
mystring
as"Python is a popular programming language"
- Then we have applied
partition()
method on the string . - From the output we can see that the returned tuple contains
'Python is '
(left part of the string) ,'a'
(separator) and' popular programming language'
(right part of the string)
Example 2:
# declare a string
mystring="Python is a popular programming language"
# split the string based on first "xyz"
mytuple=mystring.partition("xyz")
print(mytuple)
('Python is a popular programming language', '', '')
In the above code as separator "xyz"
is not found in the string so the returned tuple contains the entire string followed by two empty strings .
Python String rpartition() method
It returns a tuple containing the left part of the string after splitting by the specified separator on its first occurrence , specified separator and the right part of the string (starting from right side).
Syntax of rpartition() method
string.rpartition(sep)
Parameters of rpartition() method
- sep = It is a string based on which the given string will be separated . If the separator is not found then the returned tuple will contain two empty strings followed by the entire string .
Return type of rpartition() method
It returns a tuple .
Examples of Python String rpartition() method
Example 1:
# declare a string
mystring="Python is a popular programming language"
# split the string based on last "a"
mytuple=mystring.rpartition("a")
print(mytuple)
('Python is a popular programming langu', 'a', 'ge')
- In the above code first we have declared a string named
mystring
as"Python is a popular programming language"
- Then we have applied
partition()
method on the string . - From the output we can see that the returned tuple contains
'Python is a popular programming langu'
(left part of the string) ,'a'
(separator) and'ge'
(right part of the string)
Example 2:
# declare a string
mystring="Python is a popular programming language"
# split the string based on last "xyz"
mytuple=mystring.rpartition("xyz")
print(mytuple)
('', '', 'Python is a popular programming language')
In the above code as separator "xyz"
is not found in the string so the returned tuple contains two empty strings followed by the entire string .
Python String splitlines() method
It returns a list after breaking a string in every line boundary .
Syntax of splitlines() method
string.splitlines([keepends])
Parameters of splitlines() method
- keepends = Optional . It is a boolean . If it is
True
then line breaks will be included in the split lines otherwise not .Default value isFalse
.
Return type of splitlines() method
It returns a list .
Examples of Python String splitlines() method
Example 1:
# declare a string
mystring='''Python is a popular
programming language'''
# split the string based on newline
mylist=mystring.splitlines()
print(mylist)
['Python is a popular ', ' programming language']
Example 2:
# declare a string
mystring='''Python is a popular
programming language'''
# split the string based on newline
mylist=mystring.splitlines(True)
print(mylist)
['Python is a popular \n', ' programming language']
In the above code as keepends
parameter is True
so from the output we can see that line break ("\n"
) is included in split lines .
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .