I have an excel data. Where I have name ‘Roger Smith’. However, I would like to have ‘rsmith’.
Therefore, first alphabet of first name and family name. How can I have it in python?
>Solution :
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.