0

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 ??

1 Answer 1

7

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.

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

2 Comments

Cool !! Can you also explain the reason behind its working ??
@SaurabhVerma: it is a documented conversion character; %% is converted to %.

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.