tkbx
tkbx

Reputation: 16305

Splitting string into strings

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

Answers (2)

Brenden Brown
Brenden Brown

Reputation: 3235

>>> first, middle, last, dob = 'john..doe.1985'.split('.')
>>> first
'john'
>>> middle
''
>>> last
'doe'
>>> dob
'1985'

Upvotes: 7

Fedor Gogolev
Fedor Gogolev

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

Related Questions