如何为 raw_input 设置默认的可编辑字符串? [英] How to set a default editable string for raw_input?
本文介绍了如何为 raw_input 设置默认的可编辑字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 Python 2.7 的 raw_input
从 stdin 中读取数据.
I'm using Python 2.7's raw_input
to read from stdin.
我想让用户更改给定的默认字符串.
I want to let the user change a given default string.
代码:
i = raw_input("Please enter name:")
控制台:
Please enter name: Jack
应该向用户显示 Jack
,但可以将其更改(退格)为其他内容.
The user should be presented with Jack
but can change (backspace) it to something else.
请输入名称:
参数将是 raw_input
的提示,该部分不应由用户更改.
The Please enter name:
argument would be the prompt for raw_input
and that part shouldn't be changeable by the user.
推荐答案
你可以这样做:
i = raw_input("Please enter name[Jack]:") or "Jack"
这样,如果用户只按回车键而不输入任何内容,i"将被分配为Jack".
This way, if user just presses return without entering anything, "i" will be assigned "Jack".
这篇关于如何为 raw_input 设置默认的可编辑字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文