By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 2,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

Printing a percent sign

P: n/a
Hi all. How do I escape the "%" sign in a print statement so that it
prints? Thanks.

Stephen

Sep 25 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
wrote in news:11**********************@d34g2000cwd.googlegr oups.com in
comp.lang.python:
Hi all. How do I escape the "%" sign in a print statement so that it
prints? Thanks.
print "%%"

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Sep 25 '06 #2

P: n/a
st*****@theboulets.net wrote:
Hi all. How do I escape the "%" sign in a print statement so that it
prints? Thanks.
>>print "%"
%

Did you mean in a string being interpolated with the % operator?

Georg
Sep 25 '06 #3

P: n/a
Rob Williscroft wrote in news:Xns9849DC7DB4102rtwfreenetREMOVEcouk@
216.196.109.145 in comp.lang.python:
wrote in news:11**********************@d34g2000cwd.googlegr oups.com in
comp.lang.python:
>Hi all. How do I escape the "%" sign in a print statement so that it
prints? Thanks.

print "%%"
Ok, confused by the simplicity of the question.

Real answer is:

print "%"

But the real question was "how to print a % whern doing % formating",

acuracy = 100
print "this is %d%% more acurate than my previous answer" % acuracy
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Sep 25 '06 #4

P: n/a
st*****@theboulets.net wrote:
Hi all. How do I escape the "%" sign in a print statement so that it
prints? Thanks.
print doesn't do anything with percent signs:
>>print "%"
%

if you're doing string formatting using the "string % tuple" operator,
use two percent signs to get a percent sign in the output:
>>print "%s: %d%%" % ("level", 48)
level: 48%

</F>

Sep 25 '06 #5

P: n/a
Thanks -- a percent escapes itself when using %-formatting.

Stephen

step...@theboulets.net wrote:
Hi all. How do I escape the "%" sign in a print statement so that it
prints? Thanks.

Stephen
Sep 25 '06 #6

P: n/a

st*****@theboulets.net wrote:
Thanks -- a percent escapes itself when using %-formatting.

Stephen

step...@theboulets.net wrote:
Hi all. How do I escape the "%" sign in a print statement so that it
prints? Thanks.
The following methods of getting answers to problems can be handy if
it's non-peak hours on the net or your internet connection is
broken/slow :

1. Reasoning: How do you get a literal "'" into an SQL string constant?
How do you get a literal "\" into a Python string constant? How do you
get a literal "$" into some *x shell command lines? Do you detect a
pattern?

2. Inspecting the documentation: in this case, it says:
"""% <tabNo argument is converted, results in a "%" character in the
result. """
If that is not sufficiently clear, can you suggest how it might be
improved?

HTH generally,
John

Sep 26 '06 #7

P: n/a
In message <11*********************@d34g2000cwd.googlegroups. com>, John
Machin wrote:
1. Reasoning: How do you get a literal "'" into an SQL string constant?
How do you get a literal "\" into a Python string constant? How do you
get a literal "$" into some *x shell command lines? Do you detect a
pattern?
None of which applies to escaping of % characters in format strings.
Sep 26 '06 #8

P: n/a
Lawrence D'Oliveiro wrote:
In message <11*********************@d34g2000cwd.googlegroups. com>, John
Machin wrote:
>1. Reasoning: How do you get a literal "'" into an SQL string constant?
How do you get a literal "\" into a Python string constant? How do you
get a literal "$" into some *x shell command lines? Do you detect a
pattern?

None of which applies to escaping of % characters in format strings.
Its the pattern of escaping here, and yes, it applies: usually, a escaping
character can be literally inserted by doubling it. I'm currently a bit
unsure of the single-quote for sql though, but I'm oscillating between ''
or '''. So - it applies.

Diez
Sep 26 '06 #9

P: n/a

Lawrence D'Oliveiro wrote:
In message <11*********************@d34g2000cwd.googlegroups. com>, John
Machin wrote:
1. Reasoning: How do you get a literal "'" into an SQL string constant?
How do you get a literal "\" into a Python string constant? How do you
get a literal "$" into some *x shell command lines? Do you detect a
pattern?

None of which applies to escaping of % characters in format strings.
What I had in mind was:

where surname = 'O''REILLY'
install_dir = "C:\\Python25"
....
print "The interest rate is %.2f%% p.a." % (rate * 100.0)

the common pattern being that the problem character is doubled.

Sep 26 '06 #10

P: n/a
In message <11**********************@m7g2000cwm.googlegroups. com>, John
Machin wrote:
Lawrence D'Oliveiro wrote:
>In message <11*********************@d34g2000cwd.googlegroups. com>, John
Machin wrote:
1. Reasoning: How do you get a literal "'" into an SQL string constant?
How do you get a literal "\" into a Python string constant? How do you
get a literal "$" into some *x shell command lines? Do you detect a
pattern?

None of which applies to escaping of % characters in format strings.

What I had in mind was:

where surname = 'O''REILLY'
install_dir = "C:\\Python25"
...
print "The interest rate is %.2f%% p.a." % (rate * 100.0)

the common pattern being that the problem character is doubled.
Which doesn't apply to the "$" character in *nix shell command lines.
Sep 26 '06 #11

P: n/a

Lawrence D'Oliveiro wrote:
In message <11**********************@m7g2000cwm.googlegroups. com>, John
Machin wrote:
Lawrence D'Oliveiro wrote:
In message <11*********************@d34g2000cwd.googlegroups. com>, John
Machin wrote:

1. Reasoning: How do you get a literal "'" into an SQL string constant?
How do you get a literal "\" into a Python string constant? How do you
get a literal "$" into some *x shell command lines? Do you detect a
pattern?

None of which applies to escaping of % characters in format strings.
What I had in mind was:

where surname = 'O''REILLY'
install_dir = "C:\\Python25"
...
print "The interest rate is %.2f%% p.a." % (rate * 100.0)

the common pattern being that the problem character is doubled.

Which doesn't apply to the "$" character in *nix shell command lines.
I'll take your word for it; it's been quite a while :-) *Something* in
the dim dark past worked like that; I thought maybe I was thinking of
m4, but that gets by without doubling.

Your score so far is 1 out of 3; you have two more to go to match your
original assertion "None of which applies...."

Cheers,
John

Sep 26 '06 #12

P: n/a
John Machin wrote:
I'll take your word for it; it's been quite a while :-) *Something* in
the dim dark past worked like that
makefiles?

</F>

Sep 26 '06 #13

P: n/a

Fredrik Lundh wrote:
John Machin wrote:
I'll take your word for it; it's been quite a while :-) *Something* in
the dim dark past worked like that

makefiles?
Bingo! Actually, double bingo!!
>From the docs for GNU Make:
"""
Because dollar signs are used to start make variable references, if you
really want a dollar sign in a target or prerequisite you must write
two of them, `$$' (see How to Use Variables). If you have enabled
secondary expansion (see Secondary Expansion) and you want a literal
dollar sign in the prerequisites lise [sic], you must actually write
four dollar signs (`$$$$').
"""

Cheers,
John

Sep 26 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.