Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 连接和分离字符串输入_Python_String - Fatal编程技术网

Python 连接和分离字符串输入

Python 连接和分离字符串输入,python,string,Python,String,需要帮助在这样的循环中获取输入 example = input("enter the example !") 然后我需要将输入添加到一个变量中,然后将其全部打印出来 在单独的行上,例如: loop cycle 1: enter the example ! test1 loop cycle 2: enter the example ! test2 loop cycle 3: enter the example ! test3 投入: 1. test1 2. test2 3. test3 一

需要帮助在这样的循环中获取输入

example = input("enter the example !")
然后我需要将输入添加到一个变量中,然后将其全部打印出来 在单独的行上,例如:

loop cycle 1:
enter the example ! test1
loop cycle 2:
enter the example ! test2
loop cycle 3: 
enter the example ! test3
投入:

1. test1
2. test2
3. test3
一件事是我无法使用。由于在我的案例中使用列表而导致的追加是
不是在最高效率。(可能需要教授如何使用\n)

您可以将新行字符附加到输入函数中

用于蟒蛇2。

example = ""
for i in range(1,4):   
    example = example + str(i)+". " +raw_input("enter the example !") +"\n"
print example
用于蟒蛇3

example = ""
for i in range(1,4):   
    example = example + str(i)+". " +input("enter the example !") +"\n"
print (example)
输出

messi@messi-Hi-Fi-B85S3:~/Desktop/soc$ python sample.py 
enter the example !text 1
enter the example !text 2
enter the example !text 2
1. text 1
2. text 2
3. text 2

您可以将新行字符附加到输入函数中

用于蟒蛇2。

example = ""
for i in range(1,4):   
    example = example + str(i)+". " +raw_input("enter the example !") +"\n"
print example
用于蟒蛇3

example = ""
for i in range(1,4):   
    example = example + str(i)+". " +input("enter the example !") +"\n"
print (example)
输出

messi@messi-Hi-Fi-B85S3:~/Desktop/soc$ python sample.py 
enter the example !text 1
enter the example !text 2
enter the example !text 2
1. text 1
2. text 2
3. text 2

您可以使用
+=
运算符将附加输入附加到同一字符串上

但是,您需要在首次使用
+=
之前声明变量

例如:

example = ""

for num in range(1,4):
    print "loop cycle {}:".format(num)
    example += "{}. {}".format(num, input("enter the example !"))
    if num < 3:
        example += "\n"

print example
example=“”
对于范围(1,4)中的num:
打印“循环周期{}:”.format(num)
示例+=“{}.{}”。格式(num,输入(“输入示例!”)
如果num<3:
示例+=“\n”
打印示例

编辑:更新为包含条件新行和输入编号

您可以使用
+=
运算符将附加输入附加到同一字符串上

但是,您需要在首次使用
+=
之前声明变量

例如:

example = ""

for num in range(1,4):
    print "loop cycle {}:".format(num)
    example += "{}. {}".format(num, input("enter the example !"))
    if num < 3:
        example += "\n"

print example
example=“”
对于范围(1,4)中的num:
打印“循环周期{}:”.format(num)
示例+=“{}.{}”。格式(num,输入(“输入示例!”)
如果num<3:
示例+=“\n”
打印示例

编辑:更新为包含有条件的新行和输入数字

我目前正在测试这个,到目前为止它正在运行。我会再测试一点,如果成功的话,我会让你的答案成为解决方案。听起来不错。我该怎么加1呢。2.3.在每一行的开头,等等?很抱歉再次打扰您,但这会是怎样的:example=example+str(i)+“+raw\u输入(“输入示例!”)+“\n”查找伪代码似乎您正在使用python 3我目前正在测试这一点,到目前为止它正在解决。我会再测试一点,如果成功的话,我会让你的答案成为解决方案。听起来不错。我该怎么加1呢。2.3.在每一行的开头是等吗?很抱歉再次打扰您,但这是怎么做的:example=example+str(i)+“+raw\u输入(“输入示例!”)+“\n”查找伪代码似乎您正在唱python 3我如何添加1。2.3.在每条线路的起点处设置etc?(打印时)更新为包含循环编号如何添加1。2.3.在每条线路的起点处设置etc?(打印时)更新以包含循环编号这是某种家庭作业吗?您正在使用Python3吗?如果不是,为什么?这是家庭作业吗?您正在使用Python3吗?若否,原因为何?