L M
L M

Reputation: 65

How to alter user input?

So atm I'm making a table in python, and for it, I need the user to supply a name of a person for the table (e.g. David Beckham). However when the user has entered this and the table appears, the name needs to look like this: Beckham, David. How would I go about doing this?

Upvotes: 0

Views: 86

Answers (2)

jpp
jpp

Reputation: 164693

With Python 3.6+ you can use formatted string literals (PEP 498). You can use str.rsplit with maxsplit=1 to account for middle names:

x = 'David Robert Bekham'

first_names, last_name = x.rsplit(maxsplit=1)

res = f'{last_name}, {first_names}'

# 'Bekham, David Robert'

Upvotes: 1

burgerhex
burgerhex

Reputation: 1048

Just store the input in a variable:

name = input()

first_name, last_name = name.split(" ")

table_value = last_name + ", " + first_name

Upvotes: 0

Related Questions