简体   繁体   English

Python - 格式化,以便在打印文本时出现在右侧而不是左侧

[英]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')
  • '>' means align right. '>' 表示右对齐。
  • '10' is the character length you deisre. '10' 是您想要的字符长度。

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.

相关问题 Python:右侧的字符串格式 - Python: String formatting to the right side Python:在作业的左侧和右侧建立索引 - Python: Indexing the left side and right side of an assignment 从右侧打印文本 - Print text from right side 为什么这段代码中的“temp”在做右侧时会保留左侧的值? - Why does "temp" in this piece of code keep the value from the left side when doing right side? Python Kivy:将文本与标签的左侧对齐 - Python Kivy: Align text to the left side of a Label 组合数据帧,以使左侧的列条目等于右侧的列条目,如果不返回null - Combining dataframes so that column entries in left side equal column entries in right side and if not return null 在reportlab python 中将文本对齐到右侧 - Align text on the right side in reportlab python 为什么不使用Zelle图形进行轮子动画(从屏幕左侧移动到右侧)Python 3 - Why isn't wheel animating(moving from left side of screen to the right) Python 3 using zelle graphics networkx 在左侧和右侧绘制单独的节点 - graphviz,python - networkx draw separate nodes to left and right side - graphviz, python 如何使用 Python 和 DLib 找到线的左侧或右侧的点? - how to find a point is on left or right side of line using Python and DLib?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM