[英]User input into a string Python 2.7
I'm trying to enter user input into a string in two places in python 2.7.12 I want it to look something like this 我正在尝试在python 2.7.12中的两个位置将用户输入输入到字符串中,我希望它看起来像这样
import os
1 = input()
2 = input()
print os.listdir("/home/test/1/2")
I know you can use .format() to input into string but the only way I know how to do it is 我知道您可以使用.format()输入字符串,但是我知道怎么做的唯一方法是
print os.listdir("/home/test/{0}".format(1))
but I couldn't figure out how to enter a second input into the string. 但我不知道如何在字符串中输入第二个输入。 sorry for any confusion, I'm kinda new to Stack Overflow.
对于任何混乱,我们深表歉意,我是Stack Overflow的新手。 If you have any questions please ask.
如果您有任何问题,请询问。
import os
segment1 = input()
segment2 = input()
print os.listdir("/home/test/{}/{}".format(segment1, segment2))
1
and 2
are not legal variable names, so 1 = input()
will cause an error. 1
和2
不是合法的变量名称,因此1 = input()
将导致错误。
You can use as many variables as you want in your format string; 您可以在格式字符串中使用任意多个变量。 just pass them as additional parameters to
.format(...)
. 只需将它们作为附加参数传递给
.format(...)
。 In the format string, you can use {0}
, {1}
, etc., or you can just use {}
positionally. 在格式字符串中,您可以使用
{0}
, {1}
等,也可以仅在位置上使用{}
。 (The first {}
refers to the first parameter, the second {}
to the second parameter, etc.). (第一
{}
指的是第一参数,所述第二{}
到第二参数,等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.