split string in python
it's very easy to split string by any separator in python.
I will introduce the way to split string in python.
str that is built-in type in python has split method.
you can pass any separator to sep parameter key. it can be also string like "<>".
and there is another one for parameter, maxsplit.
you can define how many string will be splited. the default value is -1 that means string will be splited at most.
but if you set the parameter to any value, the string will be splited at most that value.
for example, if you set the value to 2, the number of elements will be 3 at most as the result of operation.
let's check them.
I will introduce the way to split string in python.
str that is built-in type in python has split method.
str.split(sep=None, maxsplit=-1)
you can pass any separator to sep parameter key. it can be also string like "<>".
and there is another one for parameter, maxsplit.
you can define how many string will be splited. the default value is -1 that means string will be splited at most.
but if you set the parameter to any value, the string will be splited at most that value.
for example, if you set the value to 2, the number of elements will be 3 at most as the result of operation.
let's check them.
1
2
3
4
5
6
7
|
sampleString = "a,b,c,d,e"
sampleString2 = "1<>2<>3<>4<>5"
print(sampleString.split(","))
print(sampleString.split(",",2))
print(sampleString2.split("<>"))
| cs |
Comments
Post a Comment