Good afternoon.

Tell me please.I have a database and a table from which I take the name and surname, but in the database not all users with a name and a surname and I want to break out what I pull from one variable to 2.Now I do this:
fullname="Vasya Pupkin"
firstname, lastname=fullname.split()

In this situation, everything works correctly, but those with only one word come across and then errors are made
fullname="Vasya"
firstname, lastname=fullname.split()

How, then, to be? How to make, instead of the second word substitute an empty space or just did nothing?

Thanks in advance.

2 Answers 2

firstname, lastname=(fullname.split() +[''])[: 2]
so firstname, * lastname=fullname.split()but note that you may have 3 or more splits, you can do it differently, for example, by maxsplit https://docs.python.org/3/library/stdtypes.html#st ...