简体   繁体   English

如何将光标设置回python的前一行?

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

In one part of my Py code, I have to print an specific text per each char in a list.在我的 Py 代码的一部分中,我必须为列表中的每个字符打印一个特定的文本。 for example:例如:

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

but as you probably know, The cursor goes to the next line after each "print" so these will be written in three lines.但正如您可能知道的那样,每次“打印”后光标会转到下一行,因此这些将被写成三行。 Do you know anyway to set the cursor back?你知道无论如何要设置光标吗? (With just a "\\t" after each print) (每次打印后只有一个“\\t”)

Use the end parameter of the print function (default value of the end parameter is '\\n' ) :使用end的的参数print功能(的默认值end参数是'\\n' ):

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

Output:输出:

d   f   x

I advise you to concatenate your items in a string variable, and print the final concatenated items using print.我建议您将项目连接到字符串变量中,并使用 print 打印最终连接的项目。 Here is the code:这是代码:

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

You can do: print("\\t".join(dictx.values()))你可以这样做: print("\\t".join(dictx.values()))

This also does not require you to do the for loop这也不需要您执行 for 循环

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

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