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

Reverse first and last name in Python

Using an input to prompt the user, how can I reverse the first and last names, capitalize the first letter and add a comma without using a compound condition? I was able to reverse it and add a comma. The problem is I don’t want that last comma.

names=input('What is your name:').split()

for names in reversed(names):
    print(names.capitalize + ',', end=' ')

Result is this:

What is your name:steve smith

Smith, Steve, 

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

>Solution :

Do something like this:

names = input("What is your name: ").split(" ")
print(', '.join([names[1].capitalize(), names[0].capitalize()]))
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