Saurabh Verma
Saurabh Verma

Reputation: 6718

Python String Formatter having %

It's simple when the string is something like:

s = 'test%s1'
s % 'TEST'

However, when % itself is present in the string, then I'm getting this error. eg:

s = 'test%s12%34'
s % 'TEST'
>>ValueError: incomplete format

How to handel this case ??

Upvotes: 0

Views: 64

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121356

Double the %:

s = 'test%s12%%34'

On output it'll be collapsed again to a single percent symbol:

>>> s = 'test%s12%%34'
>>> s % 'TEST'
'testTEST12%34'

From the String Formatting Operations documentation, where the various conversion characters are documented:

'%' No argument is converted, results in a '%' character in the result.

where the second % is the conversion character.

Upvotes: 7

Related Questions