2

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?

1
  • Obviously, both answers are just as good, just needed to pick one. Commented Aug 13, 2012 at 21:45

2 Answers 2

8

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.