简体   繁体   English

Python:打印跨越给定宽度的字符串,但第一部分左对齐,第二部分右对齐

[英]Python: print a string spanning to a given width, but first part left-aligned and second part right-aligned

I would like to output something like: 我想输出类似:

===>>> [FINISHED] Building sources: bla                               (1h:20m:30s)
===>>> [FINISHED] Building sources: The brown fox jumped...           (7h:05m:00s)

That is, a string filling a width of N characters, with a first part left-aligned, and a second part right-aligned. 即,一个字符串填充N字符的宽度,第一部分左对齐,第二部分右对齐。

I have a print in a function and I just want to get an easy-to-read output. 我在函数中有一个print ,我只想获得一个易于阅读的输出。 At the end of the execution of the script, I will see a few lines like the ones above. 在脚本执行的最后,我将看到几行类似于上面的几行。

Some more comments: 更多评论:

  • The two parts of the string together are never going to exceed N . 字符串的两个部分加起来永远不会超过N
  • N is a constant value. N为常数。
  • I've already got the two strings, I don't need any date formatting. 我已经有了两个字符串,不需要任何日期格式。

Is it posible to do this using Python's string `format ìn a generic way? 是否可以通过通用的方式使用Python的字符串`format'来做到这一点? Something like: 就像是:

N=80
first_part_of_the_string  = "===>>> [FINISHED] Building sources: " + some_random_text
second_part_of_the_string = my_function_to_convert_time_to_hms(time)

print("{<}{:N>}".format(first_part_of_the_string, second_part_of_the_string))

assuming you know the width of the line you can use '{:n}'.format(string) to fill the string up to n-length with spaces. 假设您知道行的宽度,则可以使用'{:n}'.format(string)将字符串填充为n个长度的空格。 This does not shorten the string if it is longer than n, as you state will never be the case. 如果字符串长于n,则不会缩短字符串,因为您声明永远不会这样。

'===>>> [FINISHED] Building sources: {:35} ({})'.format('bla', 'time')

in a similar fashion you can format the time by padding with zeroes: {:02} 您可以用类似的方式通过用零填充来格式化时间: {:02}

hour = 1
minute = 20
second = 30
prefix = '===>>> [FINISHED] Building sources: '
content = 'bla'

time = '({:02}h:{:02}m:{:02}s)'.format(hour, minute, second)
print '{}{:35} {}'.format(prefix, content, time)

printing 印刷

'===>>> [FINISHED] Building sources: bla                                 (01h:20m:30s)'

Just one way, adding the right number of spaces... 仅一种方法,添加正确数量的空格...

>>> for a, b in ('fsad', 'trwe'), ('gregrfgsd', '5435234523554'):
        print a + ' ' * (50 - len(a + b)) + b

fsad                                          trwe
gregrfgsd                            5435234523554

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

相关问题 将Python中的字符串拆分为长度固定但右对齐的块 - Split string in Python into chunks with constant length, but right-aligned Python 3 - 将楼梯从右对齐转换为LEFT ALIGNED,由#符号和空格组成 - Python 3 - convert the staircase from right-aligned to LEFT ALIGNED, composed of # symbols and spaces 打印字符串左对齐,固定宽度和后缀 - Print string left aligned with fixed width and suffix Python:固定长度字符串左/右对齐多个变量 - Python: Fixed length string multiple variables left/right aligned 如何在python中打印字符串拆分的第一部分 - How to print the first part of a string split in python 使用在Python中对齐的新行打印字符串 - Print string with new lines aligned in Python 使用 GetDist 工具的 Python 图例:将第二行的一部分向右推,而将另一部分保持在左侧 - Python legend with GetDist tool : push a part of second line to the right while keepin the other part to the left 为什么python字符串以指定宽度的格式左对齐? - Why are python strings left aligned in format with specified width? 如何打印带有嵌套for循环的右对齐三角形(无字符串加法或乘法) - How to print a right aligned triangle with nested for loops (no string addition or multiplication) 在python中打印具有对齐列的表格 - Print a table with aligned columns in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM