2

How to create this kind of strings with python:

" 2558.     "
" 2.        "
" 1224456.  "

I have integer, 11 spaces and .. First is leading space, then integer, after integer is dot, and after dot are spaces untill it reaches 11 places

I can go:

x = "{: d}{}".format(25, '.')
y = "|{:11s}|".format(x)

print:

| 25.       |

Is it possible to do that in one line and one string?

Brat Karamazov
  • 141
  • 1
  • 11
  • Possible duplicate of [How can I fill out a Python string with spaces?](https://stackoverflow.com/questions/5676646/how-can-i-fill-out-a-python-string-with-spaces) – Sayse Oct 17 '19 at 12:35
  • ```" {}".format(("{}.".format(25).ljust(11)))``` – Joan Lara Oct 17 '19 at 12:43

7 Answers7

3

All of the existing answers that suggest using arithmetic to calculate the argument to ljust technically work, but they seem unnecessarily complicated to me... It seems like it would be easier to compose the rest of the string first, and call ljust on the result, no arithmetic required.

for x in [2558, 2, 1224456, 25]:
    s = f" {x}.".ljust(11)
    print("\ninput:", x)
    print("output (no pipe):", s)
    print("output (with pipe):", "|" + s + "|")

Result:

input: 2558
output (no pipe):  2558.
output (with pipe): | 2558.     |

input: 2
output (no pipe):  2.
output (with pipe): | 2.        |

input: 1224456
output (no pipe):  1224456.
output (with pipe): | 1224456.  |

input: 25
output (no pipe):  25.
output (with pipe): | 25.       |
Kevin
  • 74,910
  • 12
  • 133
  • 166
1

String addition plus .ljust() method will do the trick:

>>> x = 25
>>> x = ' '+str(x)+'.'.ljust(11-(len(str(x)+'.')))
>>> x
' 25.       '
Joe
  • 879
  • 2
  • 6
  • 15
  • Doesn't quite match the desired output, which includes a period – Kevin Oct 17 '19 at 12:40
  • There you go :)) – Joe Oct 17 '19 at 12:41
  • Getting closer :-) But now it has the wrong number of spaces. You've got 10 spaces between the dot and the end of the string, but the OP wants 7. In other words, the padding must take into account the length of x. – Kevin Oct 17 '19 at 12:42
  • its not giving correct. You need to calculated 11-len(number) then put spaces after dot – Avinash Dalvi Oct 17 '19 at 12:44
  • Manipulate the method by adding `len(11 - of string plus dot)`. This does not account when the length is > than 11. But ultimately the idea here is sharing which method to use, try to show how it works, and anyone can figure out how to use it to solve specifically the problem they are dealing with. Not post a problem, wait for someone to code it for you :D – Joe Oct 17 '19 at 12:47
1

The string format works quite well if you want a concise solution.

Since you want a total of 11 chars, including the leading space and the period, the ljust will require 10 minus the length of the integer.

for i in [2558, 2, 1224456]:
    print(f" {i}{'.'.ljust(10-len(str(i)))}")

' 2558.     '
' 2.        '
' 1224456.  '

The single quotation above is just to indicate the start and end of the string.

Merelda
  • 1,318
  • 2
  • 12
  • 26
1

Use ljust on the result of the format method (which is just https://stackoverflow.com/a/58433145/1126841 using the format method instead of f-strings):

>>> ' {}.'.format(1224456).ljust(11)
' 1224456.  '
>>> ' {}.'.format(2558).ljust(11)
' 2558.     '
>>> ' {}.'.format(2).ljust(11)
' 2.        '
>>> ' {}.'.format(1224456).ljust(11)
' 1224456.  '
chepner
  • 497,756
  • 71
  • 530
  • 681
1

You can use the format() method in combination with the f-string:

for i in [2558, 2, 1224456, 25]:
    print('"{:<11}"'.format(f'{i}.'))

Output:

"2558.      "
"2.         "
"1224456.   "
"25.        "
Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73
0

Like this ?

integer = 25

x = f"| {integer}." + " "*11 + "|"

print(x)

output :

| 25.           |

EDIT

integer = 25
x = f"| {integer}."+" "*(11-(len(str(integer))+2))+"|"

print(x)

output :

| 25.       |
Alexall
  • 423
  • 2
  • 12
0
x = 2545
x = str(x)+'.'
x = ' '+x.ljust(11-len(x))
print x
Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53