3

I am trying to substitute a variable using format() and then format the resulting string using format().

This is what I ended up doing:

>>> '{:^50}'.format("Missing files for device : {0}".format(var))
'          Missing files for device : abc          '

where var is a variable holding 'abc'. Is there a better way to get the same result?

2 Answers 2

3

In your case, there is a simpler way:

>>> "Missing files for device : {0}".format(var).center(50)
'          Missing files for device : abc          '

Calling format twice is not necessary here.

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

Comments

1

You may join the strings and pass the joined string to format function as:

>>> center_content = ["Missing files for device :", "abc"]

>>> '{:^50}'.format(' '.join(center_content))
'          Missing files for device : abc          '

Comments

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.