简体   繁体   中英

quotations in input python

When using Python3, and doing something as simple as

x=input("Enter your name: ")

print (x)

and trying to run it, the user would have to input their name as "Steve" rather than just Steve.

Is there a way around having to input with quotations?

I think you're mistaken. In Python 3, you don't need quotation marks:

localhost-2:~ $ python3.3
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 01:25:11) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = input("Enter your name:")
Enter your name:Steve
>>> x
'Steve'

You would in the olden days of Python 2, because input basically eval s what you give it:

>>> x = input("Enter your name:")
Enter your name:Steve
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'Steve' is not defined
>>> x = input("Enter your name:")
Enter your name:"Steve"
>>> x
'Steve'

and so you'd use raw_input instead:

>>> x = raw_input("Enter your name:")
Enter your name:Steve
>>> x
'Steve'

But in Python 3, input is what raw_input used to be, and so no quotation marks are necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM