Implement a program that has a name and only has the last name and first name

Asked

Viewed 112 times

2

I made him this way:

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

But when it’s time to print, it looks like this:

Fulano , Ciclano

I’d like him to be like this without that space in the middle:

Fulano, Ciclano

2 answers

4


Use the + instead of the comma in lista[-1] which will have the expected result:

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

3

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

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

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

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

If you are working on an older version you can use string format which is similar yet not so intuitive:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.