473,416 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Printing a percent sign

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

Stephen

Sep 25 '06 #1
13 58715
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
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
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
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
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

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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bill | last post by:
I have some values that I want to display as percent, such as the retail price/wholesale price. In some instances, the wholesale price is zero, so I get a division by zero error. What can I do...
11
by: Joey Martin | last post by:
I am passing a sql string thru my querystring for the next page to capture. example: www.xxxxxxxx.com/index.asp?str=select * from table where name like '%doe%' Passing a basic string works...
4
by: MX1 | last post by:
Hi all, I've setup a table with one field that will hold percent values. The type is number and the format is percent on the field. When I do data entry directly into the field, I have to put...
1
by: Alan Lane | last post by:
Hello world: I'm having trouble finding how to escape the percent sign ("%") from a SQL query so that it will show up in an Access report. Here's my SQL string ... '--Build SQL String...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
1
by: Bostonrose | last post by:
I need to use the % per cent sign as the character not words in a title and I need to bold it but nothing I try seems to make it bold. What am I doing wrong? Thanks, bostonrose
1
by: molle | last post by:
Hi! I have database-table with different texts, in which the %-sign is used. When I retrieve the info with SQL and PHP and display the texts with echo, I see that the percent-sign is there. I...
2
by: Smartin | last post by:
Using A2003. I tried to query for records that contain a trailing '%' symbol in a certain field but was unable to get this to work. Knowing % is a wildcard character I tried substituting the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.