How to concatenate two types of different values but of the same input into a list in python?

Asked

Viewed 33 times

-3

I have the following code that reads a person’s name and age:

pessoas = []

p = [str(input('Digite o nome e a idade da pessoa no formato: "Nome 23": '))]
pessoas.append(p)

print(pessoas)

Supposing I typed in: "Marcelo 27", then pessoas = [['Marcelo 27']].

how do I make people equal to [['Marcelo', 27]]?

1 answer

0


You can use the function split. An example:

>>> "Marcelo 27".split()
['Marcelo', '27']

But note that if the name is double, as Marcos Vinicius, will not work as you expect, for it will return ['Marcos', 'Vinicius, '27']. Then I suggest you make two calls from input.

Browser other questions tagged

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