简体   繁体   English

如何使 output 打印在单独的行中?

[英]How do I make the output printed in separate lines?

i have this code我有这个代码

import random
sen_1 = input("pls enter the sent 1: ") 
sen_2 = input("pls enter the sent 2: ")
sen_3 = input("pls enter the sent 3: ")
sen_4 = input("pls enter the sent 4: ")

sent = [sen_1 , sen_2 , sen_3 , sen_4]
random.shuffle(sent)
print(sent)

instead of ["x", "x", "x", "x"]而不是["x", "x", "x", "x"]

Im expecting an output like this:我期待这样的 output :

["x", 

 "x",

 "x",

 "x"]

Thanks in advance提前致谢

You can also use this:你也可以使用这个:

print(*sent, sep = "\n") 

Finally, it will look like that:最后,它看起来像这样:

import random
sen_1 = input("pls enter the sent 1: ") 
sen_2 = input("pls enter the sent 2: ")
sen_3 = input("pls enter the sent 3: ")
sen_4 = input("pls enter the sent 4: ")

sent = [sen_1 , sen_2 , sen_3 , sen_4]
random.shuffle(sent)
print(*sent, sep = "\n")

To obtain the exact output as you described, I would first call the str () method then replace the "," with a ",\n" (comma-separator and a line-break), like so如您所述,要获得确切的 output ,我将首先调用str () 方法,然后将 "," 替换为 ",\n" (逗号分隔符和换行符),就像这样

print(str(sent).replace("," , ",\n"))

So your print output will look exactly like this:所以你的打印 output 看起来就像这样:

['x',
 'x',
 'x',
 'x']

You need to loop your array like this:你需要像这样循环你的数组:

for element in sent:
    print(element)

You could also simply join the contents of the list with the newline character您也可以简单地使用换行符加入列表的内容

print('\n'.join(sent))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM