DEV Community

Cover image for A Feature with an F-string that I Found out about by Accident
Vadim Kolobanov
Vadim Kolobanov

Posted on

A Feature with an F-string that I Found out about by Accident

From author

You can find many interesting articles on my website, please visit it Happy Python Website

Look, here is the usual f-string

text = 'JUPI'
print(f'{text}') 
JUPI
Enter fullscreen mode Exit fullscreen mode

If I add to it

  • colon,
  • placeholder char,
  • and I will specify the required string length using the <
print(f'{text:-<15}') 
Enter fullscreen mode Exit fullscreen mode

Then the rest of the line that is not occupied by the text will be filled with the selected character:

JUPI-----------

If you specify the length of the string via >, then you can fill in the line on the left side:

print(f'{text:->15}') 
Enter fullscreen mode Exit fullscreen mode

-----------JUPI

Write in the comments how to fill in the line on both sides so that it turns out

-----JUPI------

Top comments (0)