簡體   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