Skip to main content
edited example code to remove rogue quotation marks
Source Link
Eliza Weisman
  • 863
  • 10
  • 17
>>> a = input("enter something: ") # treats input as Python code enter something: 4 # will evaluate as the integer 4 >>> type(a) <type 'int'> # type of 4 is an integer >>> a = input("enter something: ") # treats input as Python code enter something: this is a string # will result in an error File "<stdin>", line 1, in <module> File "<string>", line 1 this is a string ^ SyntaxError: unexpected EOF while parsing >>> a = input("enter something: ") # treats input as Python code enter something"something: "this is a string" # "this is a string" wrapped in quotes is a string >>> type(a) <type 'str'> # a is the string "this is a string" 
>>> a = raw_input("enter something: ") # treats all input as strings "enterenter something: this is a string >>> type(a) <type 'str'> # a is the string "this is a string" >>> print(a) this is a string >>> a = raw_input("enter something: ") # treats all input as strings enter something: "this is a string" >>> type(a) <type 'str'> # a is the string ""this is a string"" >>> print(a) "this is a string" >>> a = raw_input("enter something: ") # treats all input as strings enter something: 4"4 >>> type(a) <type 'str'> # a is the string "4" 
>>> a = input("enter something: ") # treats input as Python code enter something: 4 # will evaluate as the integer 4 >>> type(a) <type 'int'> # type of 4 is an integer >>> a = input("enter something: ") # treats input as Python code enter something: this is a string # will result in an error File "<stdin>", line 1, in <module> File "<string>", line 1 this is a string ^ SyntaxError: unexpected EOF while parsing >>> a = input("enter something: ") # treats input as Python code enter something" "this is a string" # "this is a string" wrapped in quotes is a string >>> type(a) <type 'str'> # a is the string "this is a string" 
>>> a = raw_input("enter something: ") # treats all input as strings "enter something: this is a string >>> type(a) <type 'str'> # a is the string "this is a string" >>> print(a) this is a string >>> a = raw_input("enter something: ") # treats all input as strings enter something: "this is a string" >>> type(a) <type 'str'> # a is the string ""this is a string"" >>> print(a) "this is a string" >>> a = raw_input("enter something: ") # treats all input as strings enter something: 4" >>> type(a) <type 'str'> # a is the string "4" 
>>> a = input("enter something: ") # treats input as Python code enter something: 4 # will evaluate as the integer 4 >>> type(a) <type 'int'> # type of 4 is an integer >>> a = input("enter something: ") # treats input as Python code enter something: this is a string # will result in an error File "<stdin>", line 1, in <module> File "<string>", line 1 this is a string ^ SyntaxError: unexpected EOF while parsing >>> a = input("enter something: ") # treats input as Python code enter something: "this is a string" # "this is a string" wrapped in quotes is a string >>> type(a) <type 'str'> # a is the string "this is a string" 
>>> a = raw_input("enter something: ") # treats all input as strings enter something: this is a string >>> type(a) <type 'str'> # a is the string "this is a string" >>> print(a) this is a string >>> a = raw_input("enter something: ") # treats all input as strings enter something: "this is a string" >>> type(a) <type 'str'> # a is the string ""this is a string"" >>> print(a) "this is a string" >>> a = raw_input("enter something: ") # treats all input as strings enter something: 4 >>> type(a) <type 'str'> # a is the string "4" 
added examples
Source Link
Eliza Weisman
  • 863
  • 10
  • 17

Python's input() treats input as Python code - it is essentially taking raw_input(), sending it to eval() and then returning the result. This means that if you want to treat input as a string, you need to enter it surrounded with quotes. ItIf you enter text surrounded with quotes, input() attempts to treat that text as Python code. Here are some examples (copy-pasted from my Python REPL):

Using input():

>>> a = input("enter something: ") # treats input as Python code enter something: 4 # will evaluate as the integer 4 >>> type(a) <type 'int'> # type of 4 is an integer >>> a = input("enter something: ") # treats input as Python code enter something: this is a string # will result in an error File "<stdin>", line 1, in <module> File "<string>", line 1 this is a string ^ SyntaxError: unexpected EOF while parsing >>> a = input("enter something: ") # treats input as Python code enter something" "this is a string" # "this is a string" wrapped in quotes is a string >>> type(a) <type 'str'> # a is the string "this is a string" 

Using raw_input():

>>> a = raw_input("enter something: ") # treats all input as strings "enter something: this is a string >>> type(a) <type 'str'> # a is the string "this is a string" >>> print(a) this is a string >>> a = raw_input("enter something: ") # treats all input as strings enter something: "this is a string" >>> type(a) <type 'str'> # a is the string ""this is a string"" >>> print(a) "this is a string" >>> a = raw_input("enter something: ") # treats all input as strings enter something: 4" >>> type(a) <type 'str'> # a is the string "4" 

This also means that the calls to eval() are unnecessary if you use input().

Python's input() treats input as Python code - it is essentially taking raw_input(), sending it to eval() and then returning the result. This means that if you want to treat input as a string, you need to enter it surrounded with quotes. It also means that the calls to eval() are unnecessary if you use input().

Python's input() treats input as Python code - it is essentially taking raw_input(), sending it to eval() and then returning the result. This means that if you want to treat input as a string, you need to enter it surrounded with quotes. If you enter text surrounded with quotes, input() attempts to treat that text as Python code. Here are some examples (copy-pasted from my Python REPL):

Using input():

>>> a = input("enter something: ") # treats input as Python code enter something: 4 # will evaluate as the integer 4 >>> type(a) <type 'int'> # type of 4 is an integer >>> a = input("enter something: ") # treats input as Python code enter something: this is a string # will result in an error File "<stdin>", line 1, in <module> File "<string>", line 1 this is a string ^ SyntaxError: unexpected EOF while parsing >>> a = input("enter something: ") # treats input as Python code enter something" "this is a string" # "this is a string" wrapped in quotes is a string >>> type(a) <type 'str'> # a is the string "this is a string" 

Using raw_input():

>>> a = raw_input("enter something: ") # treats all input as strings "enter something: this is a string >>> type(a) <type 'str'> # a is the string "this is a string" >>> print(a) this is a string >>> a = raw_input("enter something: ") # treats all input as strings enter something: "this is a string" >>> type(a) <type 'str'> # a is the string ""this is a string"" >>> print(a) "this is a string" >>> a = raw_input("enter something: ") # treats all input as strings enter something: 4" >>> type(a) <type 'str'> # a is the string "4" 

This also means that the calls to eval() are unnecessary if you use input().

Source Link
Eliza Weisman
  • 863
  • 10
  • 17

Python's input() treats input as Python code - it is essentially taking raw_input(), sending it to eval() and then returning the result. This means that if you want to treat input as a string, you need to enter it surrounded with quotes. It also means that the calls to eval() are unnecessary if you use input().

I'd use raw_input() so that the user can enter input without quotation marks, and continue using eval() as you are now.