I'm afraid this is a basic Python issue, but I couldn't find a proper answer to this. I want to add a single padding space in front of a placeholder.
This is my example:
user = 'Jhon'
'Hi {u}!'.format(u=user if user else '')
# result
'Hi Jhon!'
Now everything is fine until my user var is empty or false, in that case this is the result of above
'Hi !'
# ^ notice the empty space
Instead I want 'Hi!' as a result.
Now I've tried with format() padding options like {:>1} but ofc is not working since it will add enough padding to reach a character length of 1.
I ended up doing something like this:
'Hi{u:>{p}}!'.format(u=user if user else '', p=1+len(user))
The above works fine, but I think it's kinda hackish and I'd like to now if there is a inbuilt way of doing this that I'm missing.
Thank you!
'Hi {u}!'.format(u=user) if user else 'Hi!'– easy to read and flexible.'Hi{u}!'.format(u=' ' + user if user else '')?