Search code examples
pythonstringinputquotes

Does Python input need to be surrounded by quotes?


This should be simple, but I'm having trouble finding an explanation. Here's my code:

 car = input("What kind of car would you like? ")
    print("\nLet me see if I can find a {}".format(car))

The terminal then prompts for a type of car.

I enter: Ford

I get this error:

What kind of car would you like? Ford
Traceback (most recent call last):
  File "rental_car.py", line 1, in <module>
    car = input("What kind of car would you like? ")
  File "<string>", line 1, in <module>
NameError: name 'Ford' is not defined

If I enter: 'Ford' The program runs as expected.

Does Python require quotes on input values other than numbers?

Any help is appreciated!


Solution

  • On Python 2.x, the function input evaluates whatever the user enters. As the docs state, this is equivalent to eval(raw_input(...)).

    So on Python 2.x you should use raw_input instead.

    Or, for compatibility between Python 2 and 3, you can use six.moves.input.