简体   繁体   English

如何在Python 3中不对字符串本身进行计数来对字符串进行对齐?

[英]How do I right justify string in Python 3 not counting the string itself?

Just started learning python. 刚开始学习python。 And it looks to me that rjust() method is counting string that I'm trying to justify in justifying length. 在我看来, rjust()方法正在计算我要证明其长度合理的字符串。

Let me clarify a little bit, the book I'm using to study python had this type of small program: 让我澄清一下,我用来学习python的书中有这种类型的小程序:

order = "First\nSecond\nThird"
print("The order is: ", order)

Whose output is: 其输出是:

The order is: First  
Second  
Third

And I thought what could I change to make output as ( desired output ), 而且我认为我可以更改些什么以使输出成为( 所需的输出 ),

The order is: First   
              Second   
              Third  

Well there may be very easy way to solve this but bear in mind I just started learning python. 好吧,也许有一种非常简单的方法来解决这个问题,但是请记住,我刚刚开始学习python。 I tried formatting to no avail. 我尝试格式化无济于事。 After learning about list I came up with this solution, 在了解列表之后,我想到了这个解决方案,

order = "First\nSecond\nThird"

listed_order = order.split("\n")
length_of_list = len(listed_order)

print_line = "The order is: "
length = len(print_line)

print(print_line + listed_order[0])
for i in range(1, length_of_list):
    print(listed_order[i].rjust(length))

But the output is, 但是输出是

The order is: First
        Second
         Third

That means rjust() method is counting Second and Third to make justified length of 14, while I wanted them to start from 14. Second has 8 spaces in front of it to make justified length of 14, while Third has 9 spaces. 这意味着rjust()方法将Second和Third计数为14的合理长度,而我希望它们从14开始。Second前面有8个空格,以使14的合理长度为参数,而Third则有9个空格。

So, what can I do to get my desired output? 那么, 我该怎么做才能获得所需的输出? If there is another way to solve this let me know. 如果还有其他解决方法,请告诉我。

What you describe is left -justified, but with an additional offset (also known as indentation ). 您所描述的内容是对齐的,但是具有附加的偏移量(也称为缩进 )。

Instead of 代替

print(listed_order[i].rjust(length))

simply do 简单地做

print(' ' * length + listed_order[i])

If there is nothing to the right of listed_order[i] , you don't need to use ljust either. 如果listed_order[i]右边没有任何内容,则无需使用ljust

You might want to take a look at textwrap.indent , which lets you indent whole blocks of text without splitting the lines manually. 您可能需要看一下textwrap.indent ,它使您可以缩进整个文本块而无需手动分割行。

You don't have to do such things: length_of_list = len(listed_order) . 您不必执行以下操作: length_of_list = len(listed_order) You can run your loop without checking length of the list: 您可以运行循环而无需检查列表的长度:

for i in listed_order:
    print(i.rjust(length))

EDIT: As pointed in the comments, this would also print first element which we want to skip. 编辑:正如评论中指出的那样,这还将打印我们要跳过的第一个元素。 You can do it by telling python to process from second element by adding [1:] , which means from element of index 1 (which is second after index 0) untill end) , so: 您可以通过添加[1:]来告诉python从第二个元素开始进行处理,这意味着from element of index 1 (which is second after index 0) untill end) ,因此:

for i in listed_order[1:]:
    print(i.ljust(length))

Now to the core. 现在到了核心。

What you need is the number of whitespaces. 您需要的是空格数量。 You can get it by running this: 您可以通过运行以下命令获取它:

number_of_whitespaces = len("The order is: ")

And then print it out before each line: 然后在每一行之前将其打印出来:

print(' ' * number_of_whitespaces, end='')

Full solution here: 完整解决方案在这里:

#!/usr/bin/env python3

order = "First\nSecond\nThird"
print("The order is: ", order)

orders = order.split()
whitespaces = len('The order is: ')

print('The order is: {}'.format(orders[0]))
for o in orders[1:]:
    print(' ' * whitespaces, end='')
    print(o)

EDIT2: Added missing colon, Changed rjust to ljust EDIT3: Added full solution EDIT2:添加了缺少的冒号,更改为正好调整为EDIT3:添加了完整的解决方案

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

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