Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python split string into multiple string [duplicate]

Possible Duplicate:
Split string into a list in Python

I have a string with a lot of part-strings

>>> s = 'str1, str2, str3, str4'

now I have a function like following

>>> def f(*args):
        print(args)

what I need, is to split my string into multiple strings, so that my function prints something like this

>>> f(s)
('str1', 'str2', 'str3', 'str4')

Has someone an idea, how I can perform this?

  • edit: I did not searched a function to split a string into an array of Strings.

This is what i searched for.

>>> s = s.split(', ')
>>> f(*s)
('str1', 'str2', 'str3', 'str4')
like image 992
knumskull Avatar asked Dec 21 '25 12:12

knumskull


2 Answers

You can split a string by using split(). The syntax is as follows...

stringtosplit.split('whattosplitat')

To split your example at every comma and space, it would be:

s = 'str1, str2, str3, str4'
s.split(', ')
like image 175
CoffeeRain Avatar answered Dec 23 '25 00:12

CoffeeRain


A bit of google would have found this..

string = 'the quick brown fox'
splitString = string.split()

...

['the','quick','brown','fox']
like image 28
tbd Avatar answered Dec 23 '25 02:12

tbd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!