如何为raw_input设置默认的可编辑字符串? [英] How to set a default editable string for raw_input?
本文介绍了如何为raw_input设置默认的可编辑字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Python 2.7的raw_input
从标准输入中读取.
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.
Please enter name:
参数将是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"分配杰克".
This way, if user just presses return without entering anything, "i" will be assigned "Jack".
这篇关于如何为raw_input设置默认的可编辑字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文