❮ 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
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']
How to split this string where __
is the delimiter
MATCHES__STRING
To get an output of ['MATCHES', 'STRING']
?
shgnInc
1,9021 gold badge22 silver badges33 bronze badges
asked Aug 13, 2010 at 8:46
2
You can use the str.split
method: string.split['__']
>>> "MATCHES__STRING".split["__"]
['MATCHES', 'STRING']
MendelG
10.4k3 gold badges19 silver badges36 bronze badges
answered Aug 13, 2010 at 8:48
adamkadamk
43.4k7 gold badges49 silver badges56 bronze badges
4
You may be interested in the csv
module,
which is designed for comma-separated files but can be easily modified to use a custom delimiter.
import csv
csv.register_dialect[ "myDialect", delimiter = "__", ]
lines = [ "MATCHES__STRING" ]
for row in csv.reader[ lines ]:
...
Aran-Fey
36.5k11 gold badges96 silver badges141 bronze badges
answered Aug 13, 2010 at 8:56
KatrielKatriel
116k19 gold badges133 silver badges165 bronze badges
When you have two or more elements in the string [in the example below there are three], then you can use a comma to separate these items:
date, time, event_name = ev.get_text[separator='@'].split["@"]
After this line of code, the three variables will have values from three parts of the variable ev
.
So, if the variable ev
contains this string and we apply separator @
:
Sa., 23. März@19:00@Klavier + Orchester: SPEZIAL
Then, after the split
operation the
variable
date
will have valueSa., 23. März
time
will have value19:00
event_name
will have valueKlavier + Orchester: SPEZIAL
Gino Mempin
20.9k24 gold badges85 silver badges113 bronze badges
answered Mar 4, 2019 at 16:44
1
For Python 3.8, you
actually don't need the get_text
method, you can just go with ev.split["@"]
, as a matter of fact the get_text
method is throwing an att. error. So if you have a string variable, for example:
filename = 'file/foo/bar/fox'
You can just split that into different variables with comas as suggested in the above comment but with a correction:
W, X, Y, Z = filename.split['_']
W = 'file'
X = 'foo'
Y = 'bar'
Z = 'fox'
answered Sep 8, 2021 at 14:22
GnaiGnai
155 bronze badges
Besides split
and rsplit
, there is partition
/rpartition
. It separates string once, but the way question was asked, it may apply as well.
Example:
>>> "MATCHES__STRING".partition["__"]
['MATCHES', '__', 'STRING']
>>> "MATCHES__STRING".partition["__"][::2]
['MATCHES', 'STRING']
And a bit faster then split["_",1]
:
$ python -m timeit "'validate_field_name'.split['_', 1][-1]"
2000000 loops, best of 5: 136 nsec per loop
$ python -m timeit "'validate_field_name'.partition['_'][-1]"
2000000 loops, best of 5: 108 nsec per loop
Timeit lines are based on this answer
answered Apr 11 at 12:44
topin89topin89
1412 silver badges3 bronze badges