Search code examples
python-3.xstringinputquotes

If input() returns a string, why doesn't print() display the quotation marks?


I'm having trouble understanding the following:

According to my book, unless otherwise specified, input will return a string type. If a string is printed wouldn't you expect the quotes to be included in the result? Is this just how print() is designed to work, if so why?

Example problem:

x = input() # user enters 5.5

print(x) # i expect '5.5' to be printed, instead 5.5 is printed

Wouldn't it be better to print the variable x for exactly what it is?


Solution

  • No, you use quotes to create a literal string; quotes are not part of the string value itself. If you want to see the quotes, ask Python for the representation of your string, i.e.

    print(repr(x))