Problem in python print

Asked

Viewed 103 times

1

I have 2 inputs that receive the first and last name of a person, then is informed the values typed and is asked the age of the same, but the print is showing the name information between "Square brackets". How can I remove?

That’s the way out

Type your first name: Elvis
Type your last name: da silva
['Elvis'] ['Da', 'Silva'], Type your age: 18

That is the code

first_name = str(input('Type your first name: ').split())
last_name = str(input('Type your last name: ').split())

age = int(input('{}, Type your age: '.format(str(first_name+" "+str(last_name)).title())))
  • I believe you were willing to use the method .strip() to remove spaces before and after the string and ended up getting confused.

1 answer

2

The split is the cause of the "problem", when using it Voce converts the string to a list, see an example:

name = 'Foo'

print(type(name))
<class 'str'>

print(name)
Foo

name = name.split()
print(type(name))
<class 'list'>

print(name)
['Foo']

last_name = 'Bar'
last_name = str(last_name.split()) # O que vc esta fazendo

print(type(last_name))
<class 'str'>

print(last_name)
['Bar']

You could do it in many ways, one of the simplest would be:

first_name = input('Type your first name: ')
last_name = input('Type your last name: ')

age = int(input('{} {}, Type your age: '.format(first_name, last_name)))

Note that there is no need to use str() in the input pq it already automatically converts to string.

Browser other questions tagged

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