I have a spreadsheet with data. There I have a name like Roger Smith. I would like to have the user name rsmith.
Therefore, the first letter of the first name followed by the family name. How can I do this in Python?
def make_username(full_name: str) -> str:
first_names, last_name = full_name.lower().rsplit(maxsplit=1)
return first_names[0] + last_name
print(make_username("Roger M. Smith"))
Output: rsmith
The use of rsplit is to ensure that in case someone has more than one first name, the last name is still taken properly. I assume that last names will not have spaces in them.
Note however that depending on your use case, you may need to perform additional operations to ensure that you don't get duplicates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With