Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
add space on variable
#1
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
Reply
#2
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
enigma619 likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
>>> spam = 'foo'
>>> eggs = f'{spam: >10}' # eggs will have 10 chars
>>> eggs
'       foo'
>>> eggs = f'{spam: <10}'
>>> eggs
'foo       '
enigma619 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thanks for your answers it's OK for me now with your help!

Alex
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing Space between variable and string in Python coder_sw99 6 6,258 Aug-23-2022, 01:15 PM
Last Post: louries
  Remove a space between a string and variable in print sie 5 1,759 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  from global space to local space Skaperen 4 2,301 Sep-08-2020, 04:59 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020