1

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!

4
  • 3
    Avoid the issue: 'Hi {u}!'.format(u=user) if user else 'Hi!' – easy to read and flexible. Commented Mar 12, 2017 at 8:57
  • 1
    How about 'Hi{u}!'.format(u=' ' + user if user else '') ? Commented Mar 12, 2017 at 9:01
  • Both of you are right indeed, @Ryan suggestion looks quick and clear but unfortunately in my situation I can't use it (the example string was a reduced and simplified case to show my problem). Commented Mar 12, 2017 at 19:32
  • If @PM2Ring publish his suggestion as an answer I'll mark it as solution Commented Mar 12, 2017 at 19:33

1 Answer 1

1

Here's the solution I posted in the comments:

'Hi{u}!'.format(u=' ' + user if user else '')

The solution you posted in the question doesn't quite work, but it can be fixed by changing your padding length calculation to

p=bool(user) + len(user)

The bool function determines whether it's argument is True-ish or False-ish and returns True or False, respectively. But True and False can be used in arithmetic operations where they evaluate to 1 and 0, respectively.

Here's some code that demonstrates various solutions, including a couple that use literal string interpolation aka f-strings, which are available in Python 3.6+.

for user in ('John', ''):
    print('user is {!r}'.format(user)) 
    # Gruber
    print('Hi{u:>{p}}!'.format(u=user if user else '', p=1+len(user)))
    # Gruber, fixed
    print('Hi{u:>{p}}!'.format(u=user if user else '', p=bool(user) + len(user)))
    # Ryan
    print('Hi {u}!'.format(u=user) if user else 'Hi!')
    # PM 2Ring
    print('Hi{u}!'.format(u=' ' + user if user else ''))
    print('Hi{p}{u}!'.format(u=user, p=' ' if user else ''))
    # Python 3.6
    print(f'Hi{user and " " or ""}{user}!')
    print(f'Hi{" " if user else ""}{user}!')
    # Old `%-style` interpolation
    print('Hi%s%s!' % (user and " " or "", user))

    print()

output

user is 'John'
Hi John!
Hi John!
Hi John!
Hi John!
Hi John!
Hi John!
Hi John!
Hi John!

user is ''
Hi !
Hi!
Hi!
Hi!
Hi!
Hi!
Hi!
Hi!

For info about f-strings please see Formatted string literals in the official Python docs. Also see PEP 498 -- Literal String Interpolation and PEP 536 -- Final Grammar for Literal String Interpolation.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the very detailed explanation and that nice looking f-string didn't know about it! For anyone interested look this and this
@Gruber f-strings are awesome. I generally don't use them in my SO answers since not everyone has 3.6 installed (I installed the alpha version last May). But I guess I should add some links to my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.