Split string into different variables instead of array in Python
Question:
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]
Answers:
firstname, lastname = "Anonym Anonymous".split()
firstName, lastName = myString.split()
should do it if you’re sure it will return 2.
Better is firstName, lastName = myString.split(' ', 1)
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]
firstname, lastname = "Anonym Anonymous".split()
firstName, lastName = myString.split()
should do it if you’re sure it will return 2.
Better is firstName, lastName = myString.split(' ', 1)