0

The following code:

i1, i2, i3 = 1234, 45, 856
print(f"{i1:<5}{i2:<5}{i3}")

displays:

1234 45   856

This is fine but what I'd like to do is to display each integer at a given position from the left of the screen.

If possible, I also would like to keep using f string, not C-style formatting please.

This would allow me to easilly print something nicely aligned like:

 (1234)   (45)    (856)
 (12)     (45744) (844456)

Adding parenthesis like this with f-string is possible of course but it is a little nightmare. It would be much easier to provide the hardcoded position on the line where to print

BTW, using integers is just an example, I wish the solution worked for any type (float, boolean, arrays...).

u2gilles
  • 6,888
  • 7
  • 51
  • 75
  • 2
    If what you are asking is specifically about [writing output to a specific location on a terminal](https://stackoverflow.com/questions/7392779/is-it-possible-to-print-a-string-at-a-certain-screen-position-inside-idle), fstring (or any other kind of naive string formatting) does not generally deal with absolute position of some output on the screen. – metatoaster Feb 25 '19 at 06:00
  • As a matter of fact, you can use neither f string nor C-style formatting. You will have to format the string yourself. – DYZ Feb 25 '19 at 06:02

3 Answers3

2

I eventually found a workaround which consists in using encapsulated f strings:

i1, i2, i3 = 1234, 45, 856
print(f'{f"({i1})":<10}{f"({i2})":<10}{f"({i3})":<10}')
i1, i2, i3 = 12, 454, 8564
print(f'{f"({i1})":<10}{f"({i2})":<10}{f"({i3})":<10}')

output:

(1234)    (45)      (856)     
(12)      (454)     (8564)   
u2gilles
  • 6,888
  • 7
  • 51
  • 75
1

You may need to design your own formatting tool. Let's say the field for i3 starts at start. Prepare the string of the first two fields, trim or extend it, as needed, and append the string for the third item:

s12 = f"{i1:<5}{i2:<5}"
start = 8

(s12[:start] if start <= len(s) else s + " " * (start - len(s))) + f"{i3}"
#'1234 45 856'
start=12
(s12[:start] if start <= len(s) else s + " " * (start - len(s))) + f"{i3}"
#'1234 45     856'
DYZ
  • 55,249
  • 10
  • 64
  • 93
1

Not sure why do you need this but you can try this:

>>> i1, i2, i3 = 1234, 45, 856
>>> print(f"{i1:<5}{i2:<5} {i3}") # your example
1234 45    856 
>>> print(f"{i1:<5}{i2:<5} \r{i3}") # start from the begging and overwrite
8564 45
>>> print(f"{i3:>50}\r {i1:<5}{i2:<5}")
1234 45                                       856

The third case moves i3 50 chars to the right and only then prints i1 and i2. But be careful it becomes a little bit tricky to print everything in the correct way.

If you want to format your output as columns look at this Create nice column output in python or some kind of terminal counter/progress bar Text Progress Bar in the Console [closed]

alberand
  • 632
  • 5
  • 13
  • Thanks but all these cases (even the third one) erase something. I updated my post to explain why I need it – u2gilles Feb 25 '19 at 10:51
  • 1
    @u2gilles It is still not clear what you want to achieve. Why `print(f' ({i1}) ({i2}) ({i3})')` is not a solution for you? – alberand Feb 25 '19 at 10:55
  • try it with `i1, i2, i3 = 1234, 45, 856` and then with `i1, i2, i3 = 12, 454, 8564`. They are not aligned so can't be used in a nice indented table. – u2gilles Feb 25 '19 at 17:36
  • 1
    @u2gilles then what doesn't work for you? The `print(f'{i1:<10}{i2:<10}{i3:<10}')` works as expected, it prints numbers on the left and fill in empty space with spaces creating columns of witdth 10 chars – alberand Feb 25 '19 at 18:50
  • Thanks for your time. I may have asked my question with not enough details but the idea is to be able to add text around integers (or any other type, float, list...) and keep the columns. For example to display the 3 intergers with parenthesis in colums: `(1234) (45) (856)` this print losses the columns : `print(f'({i1}){"":<10}({i2}){"":<10}({i3})')`. Please try with `i1, i2, i3 = 1234, 45, 856` and `i1, i2, i3 = 12, 454, 8564`. Do you have simple solution with f-string for that? To my mind, providing a hardcoded position on the line would be great but it is not supported. – u2gilles Feb 26 '19 at 04:09
  • 1
    @u2gilles If you want to add some "text" around numbers - do it before using f-strings. – alberand Feb 27 '19 at 07:46
  • Thanks for this suggestion. But it's a few lines of python code before the print(). Not so pythonic. I hope python will add the hardcoded position option in a future release. – u2gilles Feb 27 '19 at 12:16