簡體   English   中英

如何將光標設置回python的前一行?

[英]how to set the cursor back in previous line in python?

在我的 Py 代碼的一部分中,我必須為列表中的每個字符打印一個特定的文本。 例如:

dictx{
'a': 'd',
'b': 'f',
'c': 'x'
}
x = 'abc'
for y in x:
    print(dictx[y])

但正如您可能知道的那樣,每次“打印”后光標會轉到下一行,因此這些將被寫成三行。 你知道無論如何要設置光標嗎? (每次打印后只有一個“\\t”)

使用end的的參數print功能(的默認值end參數是'\\n' ):

dictx = {
'a': 'd',
'b': 'f',
'c': 'x'
}
x = 'abc'
for y in x:
    print(dictx[y], end='\t')

輸出:

d   f   x

我建議您將項目連接到字符串變量中,並使用 print 打印最終連接的項目。 這是代碼:

dictx{
'a': 'd',
'b': 'f',
'c': 'x'
}
x = 'abc'
concatenate = ''
for y in x:
    concatenate = concatenate + '\t' + dictx[y]
print(concatenate)

你可以這樣做: print("\\t".join(dictx.values()))

這也不需要您執行 for 循環

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM