简体   繁体   English

Python:右侧的字符串格式

[英]Python: String formatting to the right side

Here is my source code: 这是我的源代码:

def fibonacci_numbers():
  how_many_numbers = int(input("\nHow many Fibbonacci numbers you want print: "))
  i = 0
  res = [0, 1]
  while i < how_many_numbers:
      res.append(sum(res[-2:]))
      print("{}. {}".format(i + 1, res[i]))
      i += 1


fibonacci_numbers()

The output now is: 现在的输出是:

How many Fibbonacci numbers you want print: 30
1. 0
2. 1
3. 1
4. 2
5. 3
6. 5
7. 8
8. 13
9. 21
10. 34
11. 55
12. 89
13. 144
...
30. 514229

But I need something like that: 但是我需要这样的东西:

How many Fibbonacci numbers you want print: 30
1.       0
2.       1
3.       1
4.       2
5.       3
6.       5
7.       8 
8.      13
9.      21
10.     34
11.     55
12.     89
13.    144
...
30. 514229

and so on. 等等。 It's depending for a numbers I choose. 这取决于我选择的数字。 If i take 50 numbers so I need more spaces, but everything need to be placed to the right side. 如果我使用50个数字,那么我需要更多的空格,但是所有内容都必须放在右侧。

How I can achieve this? 我该如何实现?

I tried with {:>width} and with .rjust method, but I can't get it. 我尝试使用{:>width}.rjust方法,但无法获取。

Is there simple solution for that? 有没有简单的解决方案?

Or maybe I need make another loop and print there? 或者,也许我需要再做一个循环并在那里打印?

Final Solution: 最终解决方案:

Thanks for answers. 感谢您的回答。 I pick up some of them and i made this: 我拿起其中一些,我做到了:

def fibonacci_numbers():

    how_many_numbers = int(input("\nHow many Fibbonacci numbers you want print: "))
    i = 0
    res = [0, 1]
    while i < how_many_numbers:
        res.append(sum(res[-2:]))
        i += 1

    size_of_i = len(str(i))
    i = 0
    for num in res[:-1]:
        size = len(str(res[-2]))
        print("{:<{width_of_i}} {:>{width}}".format(str(i)+":", num, width_of_i=size_of_i + 1, width=size))
        i += 1



fibonacci_numbers()

Change your print statement to this 将您的印刷声明更改为此

print("{}. {:10}".format(i + 1, res[i])) # for left justify
print("{}. {:>10}".format(i + 1, res[i])) # for right justify

If you need more information on how to do it, refer to this link 如果您需要更多有关操作方法的信息,请参考此链接

You can use the format-string to justify to the right. 您可以使用格式字符串在右对齐。 Try this for your print: 尝试以下方法进行打印:

print("{}. {:>20}".format(i + 1, res[i]))

Try with: 尝试:

def fibonacci_numbers():
how_many_numbers = int(input("\nHow many Fibbonacci numbers you want print: "))
i = 0
res = [0, 1]
while i < how_many_numbers:
    res.append(sum(res[-2:]))
    print("{}. \t\t{}".format(i + 1, res[i]))
    i += 1


fibonacci_numbers()

As you do not know how many digits the last number has, you have to store the results in a list and then add spaces while printing. 由于您不知道最后一个数字的位数,因此必须将结果存储在列表中,然后在打印时添加空格。

For example get the length of last number as a string 例如,获取最后一个数字的长度作为字符串

size = len(str(numbers[-1])))
padding = ""
for i in range(size):
    padding += " " 

and then loop through the array adding the needed spaces: 然后遍历数组,添加所需的空格:

for num in numbers:
    print(padding[len(str(num)):] + str(num))

You need to specify formatting width to align it to the right correctly. 您需要指定格式宽度以使其正确地向右对齐。

Code: 码:

def fibonacci_numbers(format_width):
  how_many_numbers = int(input("\nHow many Fibbonacci numbers you want print: "))
  i = 0
  res = [0, 1]
  while i < how_many_numbers:
      res.append(sum(res[-2:]))
      # Here we specify alignment to right with certain width 
      # which is >= number of digits of the largest integer in your sequence.:
      print("{}. {:>{width}}".format(i + 1, res[i], width=format_width)) 
      i += 1


fibonacci_numbers(30)

Try to format both values: 尝试格式化两个值:

import math

def fibonacci_numbers():
  how_many_numbers = int(input("\nHow many Fibbonacci numbers you want print: "))
  i = 0
  res = [0, 1]
  for i in range(how_many_numbers):
      res.append(sum(res[-2:]))
  digits = int(math.log10(res[i]))+1
  ind_digits = int(math.log10(how_many_numbers-1))+2
  for i in range(how_many_numbers):
      print("{:<{ind_digits}} {:>{digits}}".format("{}.".format(i + 1), res[i], ind_digits=ind_digits, digits=digits))


fibonacci_numbers()

UPD: support fo variable length added. UPD:支持添加可变长度。

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

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