36.3k views
2 votes
Is string default in python?

For example:

pet_type = input('Pet type (dog/cat)')

In this case, do I have to state str(input('Pet type (dog/cat)')?

1 Answer

7 votes

Answer:

No, the input function returns a string, so there is no need to use str to convert the result to a string.

For example, in the code you provided:

pet_type = input('Pet type (dog/cat)')

The value of pet_type will be a string, regardless of what the user types at the prompt.

You can verify this by printing the type of pet_type:

print(type(pet_type)) # Output: <class 'str'>

You can also use the isinstance function to check if a variable is a string:

print(isinstance(pet_type, str)) # Output: True

Step-by-step explanation:

User Alejandro Sazo
by
8.0k points