How can I input int
or string
from the user in Python.
user = input("Enter a number or name of person")
CodePudding user response:
The input stored in user
will always be a string. You can cast the input to an int()
or str()
as needed.
user = input("Enter a number or name of person: ")
# will always be <class 'str'>
print(type(user))
# convert to int
user = int(user)
# will be <class 'int'>
print(type(user))
# convert to string
user = str(user)
# will be <class 'str'>
print(type(user))
CodePudding user response:
user = input("Enter a number or name of person: ")
You want to check if it's an int
?
user = input("Enter a number or name of person: ")
try:
int(user)
except ValueError:
# The type isn't int