Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating User name from name in python

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading