Deploy a program that receives a name and displays only the last name and the first name

1

I made it this way:

nome=input('Digite seu nome completo:').title()
lista=nome.split()
print(lista)
print(lista[-1],',',lista[0])

But at the time of printing it looks like this:

Fulano , Ciclano

I would like it to stay like this without the space between:

Fulano, Ciclano
    
asked by anonymous 07.10.2018 / 21:01

2 answers

3

Use the '+' sign instead of the comma in the list [-1] you will get the expected result:

nome=input('Digite seu nome completo:').title()
lista=nome.split()
print(lista)
print(lista[-1] + ', ' + lista[0])

I hope I have helped.

    
07.10.2018 / 21:43
1

When you print multiple elements with print the function already separates all of them by space, however, you have more appropriate ways to control the formatting of what you write on the screen.

One of them is using the latest f-string , provided you have a python version of 3.6 or higher:

print(f'{lista[-1]}, {lista[0]}')

It is more readable, especially if you have multiple values that you want to display, with specific formatting.

If you're working on an older version, you can use string format which is similar though not so intuitive:

print('{}, {}'.format(lista[-1], lista[0]))
    
07.10.2018 / 23:54