Possible Duplicate:
Python Split String
Is possible to directly split string into variables in one line, instead of using two lines. I'm sure that split would have two elements. Two lines example:
myString = "Anonym Anonymous" a = myString.split() firstName,lastName = a[0],a[1]
Split String by Character In Python, we have an in-built method called list() to split the strings into a sequence of characters. The list() function accepts one argument which is a variable name where the string is stored.
Python String split() MethodThe 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.
split() only works with one argument, so I have all words with the punctuation after I split with whitespace.
firstName, lastName = myString.split()
should do it if you're sure it will return 2.
Better is firstName, lastName = myString.split(' ', 1)
firstname, lastname = "Anonym Anonymous".split()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With