1

How the following syntax can be simplified?

three='Three'
result='One-Two-%s, One-Two-%s, One-Two-%s'%(three,three,three))

Edited later:

Another surprise: getting KeyError: 'border' on this:

color='#262626'
style="QProgressBar{background-color: {ph:s}}".format(ph=color)

3 Answers 3

2
>>> three = 'Three'
>>> result = 'One-Two-{ph:s}, One-Two-{ph:s}, One-Two-{ph:s}'.format(ph=three)
>>> print result
One-Two-Three, One-Two-Three, One-Two-Three

Edit:

>>> style="QProgressBar{{background-color: {ph:s}}}".format(ph=color)
>>> print style
QProgressBar{background-color: #262626}
Sign up to request clarification or add additional context in comments.

2 Comments

Passing a variable should work too, right? result = 'One-Two-{ph:s}, One-Two-{ph:s}, One-Two-{ph:s}'.format(ph=three) where three="Three"
That's because the formatter gets confused on which {} is part of a format string and which {} is literal. For literal ones, you need to double it, i.e. {{}}
2

Try new-style formatting.

You can use a dictionary expansion to do multiple variables as well.

three='Three'
result='One-Two-{x:}, One-Two-{x:}, One-Two-{x:}'.format(**{'x': three})

Comments

2

You can use string formatting. In your example, you would do this:

three = "Three"
result = "One-Two-{0}, One-Two-{0}, One-Two-{0}".format(three)

For the other question, change it to this:

style="QProgressBar{{background-color: {0}}}".format(color)

1 Comment

I answered your other question.

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.