Python 2.7 获取用户输入并作为不带引号的字符串进行操作 [英] Python 2.7 getting user input and manipulating as string without quotations
本文介绍了Python 2.7 获取用户输入并作为不带引号的字符串进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从用户那里获取一个字符串,然后对其进行操作.
I want to get a string from a user, and then to manipulate it.
testVar = input("Ask user for something.")
有没有办法让 testVar 成为一个字符串,而不需要我让用户在引号中输入他的响应?即你好"与你好
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
如果用户输入 Hello,我会收到以下错误:
If the user types in Hello, I get the following error:
NameError: name 'Hello' 未定义
NameError: name 'Hello' is not defined
推荐答案
使用 raw_input()
而不是 input()
:
testVar = raw_input("Ask user for something.")
input()
实际上将输入计算为 Python 代码.我建议永远不要使用它.raw_input()
返回用户输入的逐字字符串.
input()
actually evaluates the input as Python code. I suggest to never use it. raw_input()
returns the verbatim string entered by the user.
这篇关于Python 2.7 获取用户输入并作为不带引号的字符串进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文