Python 2.7 getting user input and manipulating as string without quotations

Mike Lee picture Mike Lee · Feb 10, 2011 · Viewed 541.9k times · Source

I want to get a string from a user, and then to manipulate it.

testVar = input("Ask user for something.")

Is there a way for testVar to be a string without me having the user type his response in quotes? i.e. "Hello" vs. Hello

If the user types in Hello, I get the following error:

NameError: name 'Hello' is not defined

Answer

Sven Marnach picture Sven Marnach · Feb 10, 2011

Use raw_input() instead of input():

testVar = raw_input("Ask user for something.")

input() actually evaluates the input as Python code. I suggest to never use it. raw_input() returns the verbatim string entered by the user.