[英]Python - Formatting So When Text is Printed It Appears From the Right Side Not Left Side
The code below shows letter by letter of the sentence in parenthesis but it types from the left side when I run the program.下面的代码在括号中逐个字母地显示句子,但是当我运行程序时它从左侧输入。 Is there any way I can make the text print from the right side?
有什么办法可以从右侧打印文本?
from time import sleep
hello = ("Hello")
for character in hello:
print (character, end = "", flush = True)
sleep(0.1)
print(" ")
You can use str.rjust()
to right-justify a string to fit in a certain character length, padding the excess with a chosen character:您可以使用
str.rjust()
右对齐字符串以适合特定字符长度,用所选字符填充多余部分:
>>> "Hello".rjust(10, ' ')
# ' Hello'
However, you can't just right-justify everything you print in the terminal window, because you don't know how large the terminal window is going to be .但是,您不能只对在终端 window 中打印的所有内容进行右对齐,因为您不知道终端 window 的大小。 You can maybe assume the width will be 80 characters at minimum, but that's not a hard gap.
您可以假设宽度至少为 80 个字符,但这不是一个硬差距。
You'll probably get more mileage messing with your terminal's display settings than you will writing a python program to automatically right-justify your text.与编写 python 程序来自动右对齐您的文本相比,您可能会在终端的显示设置上获得更多的里程数。
You can use the format alignment options but you still have to set the string's total length though.您可以使用格式 alignment 选项,但您仍然必须设置字符串的总长度。
'{:>10}'.format('Hello')
More format options here更多格式选项在这里
In case you really need the terminal size you can extract like that .如果你真的需要终端尺寸,你可以像这样提取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.