Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating User Name from Name in Python

Tags:

python

string

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?

like image 279
OliAK Avatar asked Aug 04 '26 14:08

OliAK


1 Answers

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.

like image 145
Daniil Fajnberg Avatar answered Aug 06 '26 04:08

Daniil Fajnberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!