455

I have the following code

test = "have it break."
selectiveEscape = "Print percent % in sentence and not %s" % test
print(selectiveEscape)

I would like to get the output:

Print percent % in sentence and not have it break.

What actually happens:

 selectiveEscape = "Use percent % in sentence and not %s" % test
TypeError: %d format: a number is required, not str
Martin Thoma
139k174 gold badges689 silver badges1.1k bronze badges
asked May 21, 2012 at 0:01
8
  • 107
    Why isn't it \%? That was my guess, I'm surprised to find it's %% instead - seems pretty counterintuitive. Commented Apr 28, 2015 at 16:14
  • 5
    % i means "a decimal representation of an integer, padded left with spaces. Commented Apr 2, 2016 at 21:19
  • 8
    The escape is to the function, not the language syntax. Hence if the escape was \% it would actually be \\% when written in ordinary code. <escape><escape> is the typical pattern I've seen, and \ happens to be the most common escape character, for better or worse. Commented Oct 14, 2016 at 21:00
  • 1
    @Demis and how do you escape \ if you had to print \\%? You are bound to require escaping through repetition of special characters, if the special characters are also not special depending on circumstances. Commented Jan 17, 2017 at 21:48
  • 2
    I think it is annoying in Python that the the literal % is encoded by "%%" and not by "\%". Commented Nov 1, 2017 at 18:39

6 Answers 6

747
>>> test = "have it break."
>>> selectiveEscape = "Print percent %% in sentence and not %s" % test
>>> print selectiveEscape
Print percent % in sentence and not have it break.
answered May 21, 2012 at 0:03
Sign up to request clarification or add additional context in comments.

7 Comments

In Python 3.3.5, print('%s%%' % 100) prints 100%. But print('%%') prints %%. So it looks like you don't have to escape the % signs if you don't make substitutions.
@Zenadix This is true in Python 2.7 as well
Note that the % method is actually deprecated (in Python 3) in favor of str.format(): docs.python.org/2/library/stdtypes.html#str.format
Note that the % method is not depreciated in Python 3.6. It will continue to be supported in lieu of its similarity to c, c++, etc. str.format() and f-strings are preferred but not enforced.
Just noticed that If the string is a json string, being read from a file you don't even need to escape the % sign. Just % will do
|
63

Alternatively, as of Python 2.6, you can use new string formatting (described in PEP 3101):

'Print percent % in sentence and not {0}'.format(test)

which is especially handy as your strings get more complicated.

answered May 21, 2012 at 0:12

4 Comments

The only problem with this is when the text you want to format is HTML with a CSS style section.
What do you recommend for text formatting HTML that contains a CSS style section, @Broseph?
I was wrong. If you use double braces in your CSS you are fine.
Edit queues are full, but the http URL in this answer's link now redirects to an https URL (peps.python.org/pep-3101). Could someone edit the new URL in when the queues are less full?
52

try using %% to print % sign .

Botz3000
39.8k8 gold badges107 silver badges128 bronze badges
answered May 21, 2012 at 7:46

Comments

10

You can't selectively escape %, as % always has a special meaning depending on the following character.

In the documentation of Python, at the bottem of the second table in that section, it states:

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

Therefore you should use:

selectiveEscape = "Print percent %% in sentence and not %s" % (test, )

(please note the expicit change to tuple as argument to %)

Without knowing about the above, I would have done:

selectiveEscape = "Print percent %s in sentence and not %s" % ('%', test)

with the knowledge you obviously already had.

answered Nov 27, 2016 at 12:24

Comments

6

If you are using Python 3.6 or newer, you can use f-string:

>>> test = "have it break."
>>> selectiveEscape = f"Print percent % in sentence and not {test}"
>>> print(selectiveEscape)
... Print percent % in sentence and not have it break.
answered Jan 17, 2020 at 11:42

2 Comments

Would be nice to see how to escape { here
You just make it double {{
3

If the formatting template was read from a file, and you cannot ensure the content doubles the percent sign, then you probably have to detect the percent character and decide programmatically whether it is the start of a placeholder or not. Then the parser should also recognize sequences like %d (and other letters that can be used), but also %(xxx)s etc.

Similar problem can be observed with the new formats -- the text can contain curly braces.

answered May 22, 2012 at 7:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.