[英]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.