Jeni
Jeni

Reputation: 968

Python string formatting when already "{}" in the string

I would like to know how could I format this string:

"){e<=2}"

This string is inside a function, so I would like to asign the number to a function parameter to change it whenever I want.

I tried:

"){e<={0}}".format(number)

But it is not working, Could anybody give me some advice? Thanks in advance

Upvotes: 1

Views: 61

Answers (2)

Somebody Someone
Somebody Someone

Reputation: 15

An old-school version for this:

"){e<=%d}" % (number)
'){e<=2}'

Upvotes: 0

wstk
wstk

Reputation: 1270

Double the braces which do not correspond to the format placeholder...

"){{e<={0}}}".format(number)

You could also use an f-string, if using Python 3.6 or above.

f"){{e<={number}}}"

Upvotes: 1

Related Questions