Printing a percent sign

S

stephen

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

Stephen
 
G

Georg Brandl

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

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

Georg
 
F

Fredrik Lundh

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:
%

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

</F>
 
R

Rob Williscroft

Rob Williscroft wrote in 216.196.109.145 in comp.lang.python:
wrote in in
comp.lang.python:


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.
 
J

John Machin

Thanks -- a percent escapes itself when using %-formatting.

Stephen

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:
"""% <tab> No 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
 
L

Lawrence D'Oliveiro

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.
 
D

Diez B. Roggisch

Lawrence said:
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
 
J

John Machin

Lawrence said:
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.
 
L

Lawrence D'Oliveiro

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.
 
J

John Machin

Lawrence said:
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
 
J

John Machin

Fredrik said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top