❮ String Methods
Example
Split a string into a list where each word is a list item:
txt = "welcome to the jungle"
x = txt.split[]
print[x]
Try it Yourself »
Definition and Usage
The split[]
method splits
a string into a list.
You can specify the separator, default separator is any whitespace.
Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
Syntax
string.split[separator, maxsplit]
Parameter Values
separator | Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator |
maxsplit | Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences" |
More Examples
Example
Split the string, using comma, followed by a space, as a separator:
txt = "hello, my name is Peter, I am 26 years old"
x = txt.split[", "]
print[x]
Try it Yourself »
Example
Use a hash character as a separator:
txt = "apple#banana#cherry#orange"
x = txt.split["#"]
print[x]
Try it Yourself »
Example
Split the string into a list with max 2 items:
txt = "apple#banana#cherry#orange"
# setting the maxsplit parameter to 1, will return a list with 2 elements!
x = txt.split["#", 1]
print[x]
Try it Yourself »
❮ String Methods
Given a string, write a Python program to split the characters of the given string into a list using Python.
Examples:
Input : geeks Output : ['g', 'e', 'e', 'k', 's'] Input : Word Output : ['W', 'o', 'r', 'd']
Method 1: Split a string into a Python list using unpack[*] method
The act of unpacking involves taking things out, specifically iterables like dictionaries, lists, and tuples.
Python3
string
=
"geeks"
print
[[
*
string]]
Output:
['g', 'e', 'e', 'k', 's']
Method 2: Split a string into a Python list using a loop
Here, we are splitting the letters using the native way using the loop and then we are appending it to a new list.
Python3
string
=
'geeksforgeeks'
lst
=
[]
for
letter
in
string:
lst.append[letter]
print
[lst]
Output:
['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
Method 3: Split a string into a Python list using List Comprehension
This approach uses list comprehension to convert each character into a list. Using the following syntax you can split the characters of a string into a list.
Python3
string
=
"Geeksforgeeks"
letter
=
[x
for
x
in
string]
print
[letter]
Output:
['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
Method 4: Split a string into a Python list using a list[] typecasting
Python provides direct typecasting of strings into a list using Python list[].
Python3
def
split[word]:
return
list
[word]
word
=
'geeks'
print
[split[word]]
Output:
['g', 'e', 'e', 'k', 's']
Method 5: Split a string into a Python list using extend[]
Extend iterates over its input, expanding the list, and adding each member.
Python3
string
=
'Geeks@for'
lst
=
[]
lst.extend[string]
print
[lst]
Output:
['G', 'e', 'e', 'k', 's', '@', 'f', 'o', 'r']
Python String split[] method in Python split a string into a list of strings after breaking the given string by the specified separator.
Python String split[] Method Syntax
Syntax : str.split[separator, maxsplit]
Parameters :
- separator : This 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 the default is -1 that means there is no limit.
Returns : Returns a list of strings after breaking the given string by the specified separator.
Python String split[] Method Example
Python3
string
=
"one,two,three"
words
=
string.split[
','
]
print
[words]
Output:
['one', 'two', 'three']
Example 1: Example to demonstrate how split[] function works
Here we are using the Python String split[] function to split different Strings into a list, separated by different characters in each case.
Python3
text
=
'geeks for geeks'
print
[text.split[]]
word
=
'geeks, for, geeks'
print
[word.split[
','
]]
word
=
'geeks:for:geeks'
print
[word.split[
':'
]]
word
=
'CatBatSatFatOr'
print
[word.split[
't'
]]
Output :
['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']
Example 2: Example to demonstrate how split[] function works when maxsplit is specified
The maxsplit parameter is used to control how many splits to return after the string is parsed. Even if there are multiple splits possible, it’ll only do maximum that number of splits as defined by maxsplit parameter.
Python3
word
=
'geeks, for, geeks, pawan'
print
[word.split[
', '
,
0
]]
print
[word.split[
', '
,
4
]]
print
[word.split[
', '
,
1
]]
Output :
['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']