Reputation: 16305
Is there any way to split a string into many (not just 2) strings at a character, allowing blank strings, with the string names and order known? For example:
john..doe.1985
would split into first = 'john'
, middle = ''
, last = 'doe'
, and dob = 1985
?
Upvotes: 2
Views: 112
Reputation: 3235
>>> first, middle, last, dob = 'john..doe.1985'.split('.')
>>> first
'john'
>>> middle
''
>>> last
'doe'
>>> dob
'1985'
Upvotes: 7
Reputation: 10561
You can use split
method and iterable unpacking:
>>> first, middle, last, str_dob = "john..doe.1985".split(".")
>>> dob = int(str_dob)
>>> first
'john'
>>> middle
''
>>> last
'doe'
>>> dob
1985
Upvotes: 8