繁体   English   中英

在同一行 Python 上打印同一个单词 3 次

[英]Print the same word 3 times on the same line Python

从文件中读取后,我想在同一行上打印出 3 个相同的单词。 所以说这个词是测试。 我希望它打印TestTestTest 这是我想出的代码,但它将每个代码打印为单独的行。 我该如何解决?

import os

file = open('rockyou.txt', 'r') 
for line in file: 
    
    output = line.title() 
    print(output+output+output)

它必须有换行符:

for line in file: 
    output = line.strip().title() 
    print(output + output + output)

而不是output + output + output ,只需做乘法:

for line in file: 
    output = line.strip().title() 
    print(output * 3)

暂无
暂无

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

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