Python Forum

Full Version: add space on variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

i've a variable x which must contain 150 characters (every time)
On this variable sometimes I set just "bla" and sometimes "blablabla"
But on my result I want that my var x contains 150 characters, so I want to add "empty spaces" to my string.
Does anyone have an idea ho I can do that ?

Thanks for help
Alex
Here is one way:
def text(var):
    space = 150 - len(var)
    return f"{var + ' ' * space}<-- text plus {space} spaces"

text_list = ['Hers is some text', 'more text', 'Let\'s try even more text here']

for lines in text_list:
    print(text(lines))
Output:
Hers is some text <-- text plus 133 spaces more text <-- text plus 141 spaces Let's try even more text here <-- text plus 121 spaces
>>> spam = 'foo'
>>> eggs = f'{spam: >10}' # eggs will have 10 chars
>>> eggs
'       foo'
>>> eggs = f'{spam: <10}'
>>> eggs
'foo       '
Thanks for your answers it's OK for me now with your help!

Alex