How can I selectively escape percent (%) in Python strings?
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
python escaping python-2.7
|
show 2 more comments
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
python escaping python-2.7
26
Why isn't it%
? That was my guess, I'm surprised to find it's%%
instead - seems pretty counterintuitive.
– Demis
Apr 28 '15 at 16:14
2
% i
means "a decimal representation of an integer, padded left with spaces.
– Antti Haapala
Apr 2 '16 at 21:19
2
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, andhappens to be the most common escape character, for better or worse.
– shemnon
Oct 14 '16 at 21:00
1
@Demis and how do you escape\%
? You are bound to require escaping through repetition of special characters, if the special characters are also not special depending on circumstances.
– Sassa NF
Jan 17 '17 at 21:48
2
I think it is annoying in Python that the the literal % is encoded by "%%" and not by "%".
– Ralf
Nov 1 '17 at 18:39
|
show 2 more comments
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
python escaping python-2.7
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
python escaping python-2.7
python escaping python-2.7
edited Dec 29 '14 at 16:58
Martin Thoma
42.6k58302524
42.6k58302524
asked May 21 '12 at 0:01
jondykemanjondykeman
2,82921619
2,82921619
26
Why isn't it%
? That was my guess, I'm surprised to find it's%%
instead - seems pretty counterintuitive.
– Demis
Apr 28 '15 at 16:14
2
% i
means "a decimal representation of an integer, padded left with spaces.
– Antti Haapala
Apr 2 '16 at 21:19
2
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, andhappens to be the most common escape character, for better or worse.
– shemnon
Oct 14 '16 at 21:00
1
@Demis and how do you escape\%
? You are bound to require escaping through repetition of special characters, if the special characters are also not special depending on circumstances.
– Sassa NF
Jan 17 '17 at 21:48
2
I think it is annoying in Python that the the literal % is encoded by "%%" and not by "%".
– Ralf
Nov 1 '17 at 18:39
|
show 2 more comments
26
Why isn't it%
? That was my guess, I'm surprised to find it's%%
instead - seems pretty counterintuitive.
– Demis
Apr 28 '15 at 16:14
2
% i
means "a decimal representation of an integer, padded left with spaces.
– Antti Haapala
Apr 2 '16 at 21:19
2
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, andhappens to be the most common escape character, for better or worse.
– shemnon
Oct 14 '16 at 21:00
1
@Demis and how do you escape\%
? You are bound to require escaping through repetition of special characters, if the special characters are also not special depending on circumstances.
– Sassa NF
Jan 17 '17 at 21:48
2
I think it is annoying in Python that the the literal % is encoded by "%%" and not by "%".
– Ralf
Nov 1 '17 at 18:39
26
26
Why isn't it
%
? That was my guess, I'm surprised to find it's %%
instead - seems pretty counterintuitive.– Demis
Apr 28 '15 at 16:14
Why isn't it
%
? That was my guess, I'm surprised to find it's %%
instead - seems pretty counterintuitive.– Demis
Apr 28 '15 at 16:14
2
2
% i
means "a decimal representation of an integer, padded left with spaces.– Antti Haapala
Apr 2 '16 at 21:19
% i
means "a decimal representation of an integer, padded left with spaces.– Antti Haapala
Apr 2 '16 at 21:19
2
2
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.– shemnon
Oct 14 '16 at 21:00
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.– shemnon
Oct 14 '16 at 21:00
1
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.– Sassa NF
Jan 17 '17 at 21:48
@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.– Sassa NF
Jan 17 '17 at 21:48
2
2
I think it is annoying in Python that the the literal % is encoded by "%%" and not by "%".
– Ralf
Nov 1 '17 at 18:39
I think it is annoying in Python that the the literal % is encoded by "%%" and not by "%".
– Ralf
Nov 1 '17 at 18:39
|
show 2 more comments
6 Answers
6
active
oldest
votes
>>> test = "have it break."
>>> selectiveEscape = "Print percent %% in sentence and not %s" % test
>>> print selectiveEscape
Print percent % in sentence and not have it break.
25
In Python 3.3.5,print('%s%%' % 100)
prints100%
. Butprint('%%')
prints%%
. So it looks like you don't have to escape the % signs if you don't make substitutions.
– Zenadix
Sep 8 '15 at 19:39
3
@Zenadix This is true in Python 2.7 as well
– Tom
Dec 15 '15 at 18:17
2
Note that the%
method is actually deprecated (in Python 3) in favor ofstr.format()
: docs.python.org/2/library/stdtypes.html#str.format
– dantiston
Feb 20 '17 at 18:45
5
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.
– Aaron
Apr 7 '17 at 21:02
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
– wander95
Dec 19 '17 at 16:24
|
show 1 more comment
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.
+1, while I figured op was looking for a %-based answer I much prefer to useformat
these days.
– Nolen Royalty
May 21 '12 at 0:18
2
The only problem with this is when the text you want to format is HTML with a CSS style section.
– Broseph
Feb 13 '14 at 5:43
What do you recommend for text formatting HTML that contains a CSS style section, @Broseph?
– DucRP
Nov 10 '15 at 18:03
1
I was wrong. If you use double braces in your CSS you are fine.
– Broseph
Jan 4 '16 at 21:36
add a comment |
try using %%
to print % sign .
add a comment |
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.
add a comment |
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.
add a comment |
I have tried different methods to print a subplot title, look how they work. It's different when i use Latex.
It works with '%%' and 'string'+'%' in a typical case.
If you use Latex it worked using 'string'+'%'
So in a typical case:
import matplotlib.pyplot as plt
fig,ax = plt.subplots(4,1)
float_number = 4.17
ax[0].set_title('Total: (%1.2f' %float_number + '%)')
ax[1].set_title('Total: (%1.2f%%)' %float_number)
ax[2].set_title('Total: (%1.2f' %float_number + '%%)')
ax[3].set_title('Total: (%1.2f' %float_number + '%)')
Title examples with %
If we use latex:
import matplotlib.pyplot as plt
import matplotlib
font = 'family' : 'normal',
'weight' : 'bold',
'size' : 12
matplotlib.rc('font', **font)
matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.unicode'] = True
fig,ax = plt.subplots(4,1)
float_number = 4.17
#ax[0].set_title('Total: (%1.2f%)' %float_number) This makes python crash
ax[1].set_title('Total: (%1.2f%%)' %float_number)
ax[2].set_title('Total: (%1.2f' %float_number + '%%)')
ax[3].set_title('Total: (%1.2f' %float_number + '%)')
We get this:
Title example with % and latex
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10678229%2fhow-can-i-selectively-escape-percent-in-python-strings%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
>>> test = "have it break."
>>> selectiveEscape = "Print percent %% in sentence and not %s" % test
>>> print selectiveEscape
Print percent % in sentence and not have it break.
25
In Python 3.3.5,print('%s%%' % 100)
prints100%
. Butprint('%%')
prints%%
. So it looks like you don't have to escape the % signs if you don't make substitutions.
– Zenadix
Sep 8 '15 at 19:39
3
@Zenadix This is true in Python 2.7 as well
– Tom
Dec 15 '15 at 18:17
2
Note that the%
method is actually deprecated (in Python 3) in favor ofstr.format()
: docs.python.org/2/library/stdtypes.html#str.format
– dantiston
Feb 20 '17 at 18:45
5
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.
– Aaron
Apr 7 '17 at 21:02
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
– wander95
Dec 19 '17 at 16:24
|
show 1 more comment
>>> test = "have it break."
>>> selectiveEscape = "Print percent %% in sentence and not %s" % test
>>> print selectiveEscape
Print percent % in sentence and not have it break.
25
In Python 3.3.5,print('%s%%' % 100)
prints100%
. Butprint('%%')
prints%%
. So it looks like you don't have to escape the % signs if you don't make substitutions.
– Zenadix
Sep 8 '15 at 19:39
3
@Zenadix This is true in Python 2.7 as well
– Tom
Dec 15 '15 at 18:17
2
Note that the%
method is actually deprecated (in Python 3) in favor ofstr.format()
: docs.python.org/2/library/stdtypes.html#str.format
– dantiston
Feb 20 '17 at 18:45
5
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.
– Aaron
Apr 7 '17 at 21:02
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
– wander95
Dec 19 '17 at 16:24
|
show 1 more comment
>>> test = "have it break."
>>> selectiveEscape = "Print percent %% in sentence and not %s" % test
>>> print selectiveEscape
Print percent % in sentence and not have it break.
>>> 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 '12 at 0:03
Nolen RoyaltyNolen Royalty
14.2k33144
14.2k33144
25
In Python 3.3.5,print('%s%%' % 100)
prints100%
. Butprint('%%')
prints%%
. So it looks like you don't have to escape the % signs if you don't make substitutions.
– Zenadix
Sep 8 '15 at 19:39
3
@Zenadix This is true in Python 2.7 as well
– Tom
Dec 15 '15 at 18:17
2
Note that the%
method is actually deprecated (in Python 3) in favor ofstr.format()
: docs.python.org/2/library/stdtypes.html#str.format
– dantiston
Feb 20 '17 at 18:45
5
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.
– Aaron
Apr 7 '17 at 21:02
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
– wander95
Dec 19 '17 at 16:24
|
show 1 more comment
25
In Python 3.3.5,print('%s%%' % 100)
prints100%
. Butprint('%%')
prints%%
. So it looks like you don't have to escape the % signs if you don't make substitutions.
– Zenadix
Sep 8 '15 at 19:39
3
@Zenadix This is true in Python 2.7 as well
– Tom
Dec 15 '15 at 18:17
2
Note that the%
method is actually deprecated (in Python 3) in favor ofstr.format()
: docs.python.org/2/library/stdtypes.html#str.format
– dantiston
Feb 20 '17 at 18:45
5
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.
– Aaron
Apr 7 '17 at 21:02
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
– wander95
Dec 19 '17 at 16:24
25
25
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
Sep 8 '15 at 19:39
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
Sep 8 '15 at 19:39
3
3
@Zenadix This is true in Python 2.7 as well
– Tom
Dec 15 '15 at 18:17
@Zenadix This is true in Python 2.7 as well
– Tom
Dec 15 '15 at 18:17
2
2
Note that the
%
method is actually deprecated (in Python 3) in favor of str.format()
: docs.python.org/2/library/stdtypes.html#str.format– dantiston
Feb 20 '17 at 18:45
Note that the
%
method is actually deprecated (in Python 3) in favor of str.format()
: docs.python.org/2/library/stdtypes.html#str.format– dantiston
Feb 20 '17 at 18:45
5
5
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.– Aaron
Apr 7 '17 at 21:02
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.– Aaron
Apr 7 '17 at 21:02
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– wander95
Dec 19 '17 at 16:24
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– wander95
Dec 19 '17 at 16:24
|
show 1 more comment
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.
+1, while I figured op was looking for a %-based answer I much prefer to useformat
these days.
– Nolen Royalty
May 21 '12 at 0:18
2
The only problem with this is when the text you want to format is HTML with a CSS style section.
– Broseph
Feb 13 '14 at 5:43
What do you recommend for text formatting HTML that contains a CSS style section, @Broseph?
– DucRP
Nov 10 '15 at 18:03
1
I was wrong. If you use double braces in your CSS you are fine.
– Broseph
Jan 4 '16 at 21:36
add a comment |
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.
+1, while I figured op was looking for a %-based answer I much prefer to useformat
these days.
– Nolen Royalty
May 21 '12 at 0:18
2
The only problem with this is when the text you want to format is HTML with a CSS style section.
– Broseph
Feb 13 '14 at 5:43
What do you recommend for text formatting HTML that contains a CSS style section, @Broseph?
– DucRP
Nov 10 '15 at 18:03
1
I was wrong. If you use double braces in your CSS you are fine.
– Broseph
Jan 4 '16 at 21:36
add a comment |
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.
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 '12 at 0:12
KarmelKarmel
3,12821111
3,12821111
+1, while I figured op was looking for a %-based answer I much prefer to useformat
these days.
– Nolen Royalty
May 21 '12 at 0:18
2
The only problem with this is when the text you want to format is HTML with a CSS style section.
– Broseph
Feb 13 '14 at 5:43
What do you recommend for text formatting HTML that contains a CSS style section, @Broseph?
– DucRP
Nov 10 '15 at 18:03
1
I was wrong. If you use double braces in your CSS you are fine.
– Broseph
Jan 4 '16 at 21:36
add a comment |
+1, while I figured op was looking for a %-based answer I much prefer to useformat
these days.
– Nolen Royalty
May 21 '12 at 0:18
2
The only problem with this is when the text you want to format is HTML with a CSS style section.
– Broseph
Feb 13 '14 at 5:43
What do you recommend for text formatting HTML that contains a CSS style section, @Broseph?
– DucRP
Nov 10 '15 at 18:03
1
I was wrong. If you use double braces in your CSS you are fine.
– Broseph
Jan 4 '16 at 21:36
+1, while I figured op was looking for a %-based answer I much prefer to use
format
these days.– Nolen Royalty
May 21 '12 at 0:18
+1, while I figured op was looking for a %-based answer I much prefer to use
format
these days.– Nolen Royalty
May 21 '12 at 0:18
2
2
The only problem with this is when the text you want to format is HTML with a CSS style section.
– Broseph
Feb 13 '14 at 5:43
The only problem with this is when the text you want to format is HTML with a CSS style section.
– Broseph
Feb 13 '14 at 5:43
What do you recommend for text formatting HTML that contains a CSS style section, @Broseph?
– DucRP
Nov 10 '15 at 18:03
What do you recommend for text formatting HTML that contains a CSS style section, @Broseph?
– DucRP
Nov 10 '15 at 18:03
1
1
I was wrong. If you use double braces in your CSS you are fine.
– Broseph
Jan 4 '16 at 21:36
I was wrong. If you use double braces in your CSS you are fine.
– Broseph
Jan 4 '16 at 21:36
add a comment |
try using %%
to print % sign .
add a comment |
try using %%
to print % sign .
add a comment |
try using %%
to print % sign .
try using %%
to print % sign .
edited May 22 '12 at 6:36
Botz3000
33.3k887121
33.3k887121
answered May 21 '12 at 7:46
openmeet123openmeet123
34122
34122
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 28 '16 at 18:43
answered Nov 27 '16 at 12:24
AnthonAnthon
30.2k1793147
30.2k1793147
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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 '12 at 7:27
peprpepr
12.4k1047108
12.4k1047108
add a comment |
add a comment |
I have tried different methods to print a subplot title, look how they work. It's different when i use Latex.
It works with '%%' and 'string'+'%' in a typical case.
If you use Latex it worked using 'string'+'%'
So in a typical case:
import matplotlib.pyplot as plt
fig,ax = plt.subplots(4,1)
float_number = 4.17
ax[0].set_title('Total: (%1.2f' %float_number + '%)')
ax[1].set_title('Total: (%1.2f%%)' %float_number)
ax[2].set_title('Total: (%1.2f' %float_number + '%%)')
ax[3].set_title('Total: (%1.2f' %float_number + '%)')
Title examples with %
If we use latex:
import matplotlib.pyplot as plt
import matplotlib
font = 'family' : 'normal',
'weight' : 'bold',
'size' : 12
matplotlib.rc('font', **font)
matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.unicode'] = True
fig,ax = plt.subplots(4,1)
float_number = 4.17
#ax[0].set_title('Total: (%1.2f%)' %float_number) This makes python crash
ax[1].set_title('Total: (%1.2f%%)' %float_number)
ax[2].set_title('Total: (%1.2f' %float_number + '%%)')
ax[3].set_title('Total: (%1.2f' %float_number + '%)')
We get this:
Title example with % and latex
add a comment |
I have tried different methods to print a subplot title, look how they work. It's different when i use Latex.
It works with '%%' and 'string'+'%' in a typical case.
If you use Latex it worked using 'string'+'%'
So in a typical case:
import matplotlib.pyplot as plt
fig,ax = plt.subplots(4,1)
float_number = 4.17
ax[0].set_title('Total: (%1.2f' %float_number + '%)')
ax[1].set_title('Total: (%1.2f%%)' %float_number)
ax[2].set_title('Total: (%1.2f' %float_number + '%%)')
ax[3].set_title('Total: (%1.2f' %float_number + '%)')
Title examples with %
If we use latex:
import matplotlib.pyplot as plt
import matplotlib
font = 'family' : 'normal',
'weight' : 'bold',
'size' : 12
matplotlib.rc('font', **font)
matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.unicode'] = True
fig,ax = plt.subplots(4,1)
float_number = 4.17
#ax[0].set_title('Total: (%1.2f%)' %float_number) This makes python crash
ax[1].set_title('Total: (%1.2f%%)' %float_number)
ax[2].set_title('Total: (%1.2f' %float_number + '%%)')
ax[3].set_title('Total: (%1.2f' %float_number + '%)')
We get this:
Title example with % and latex
add a comment |
I have tried different methods to print a subplot title, look how they work. It's different when i use Latex.
It works with '%%' and 'string'+'%' in a typical case.
If you use Latex it worked using 'string'+'%'
So in a typical case:
import matplotlib.pyplot as plt
fig,ax = plt.subplots(4,1)
float_number = 4.17
ax[0].set_title('Total: (%1.2f' %float_number + '%)')
ax[1].set_title('Total: (%1.2f%%)' %float_number)
ax[2].set_title('Total: (%1.2f' %float_number + '%%)')
ax[3].set_title('Total: (%1.2f' %float_number + '%)')
Title examples with %
If we use latex:
import matplotlib.pyplot as plt
import matplotlib
font = 'family' : 'normal',
'weight' : 'bold',
'size' : 12
matplotlib.rc('font', **font)
matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.unicode'] = True
fig,ax = plt.subplots(4,1)
float_number = 4.17
#ax[0].set_title('Total: (%1.2f%)' %float_number) This makes python crash
ax[1].set_title('Total: (%1.2f%%)' %float_number)
ax[2].set_title('Total: (%1.2f' %float_number + '%%)')
ax[3].set_title('Total: (%1.2f' %float_number + '%)')
We get this:
Title example with % and latex
I have tried different methods to print a subplot title, look how they work. It's different when i use Latex.
It works with '%%' and 'string'+'%' in a typical case.
If you use Latex it worked using 'string'+'%'
So in a typical case:
import matplotlib.pyplot as plt
fig,ax = plt.subplots(4,1)
float_number = 4.17
ax[0].set_title('Total: (%1.2f' %float_number + '%)')
ax[1].set_title('Total: (%1.2f%%)' %float_number)
ax[2].set_title('Total: (%1.2f' %float_number + '%%)')
ax[3].set_title('Total: (%1.2f' %float_number + '%)')
Title examples with %
If we use latex:
import matplotlib.pyplot as plt
import matplotlib
font = 'family' : 'normal',
'weight' : 'bold',
'size' : 12
matplotlib.rc('font', **font)
matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.unicode'] = True
fig,ax = plt.subplots(4,1)
float_number = 4.17
#ax[0].set_title('Total: (%1.2f%)' %float_number) This makes python crash
ax[1].set_title('Total: (%1.2f%%)' %float_number)
ax[2].set_title('Total: (%1.2f' %float_number + '%%)')
ax[3].set_title('Total: (%1.2f' %float_number + '%)')
We get this:
Title example with % and latex
answered Apr 17 '18 at 22:29
user245554user245554
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10678229%2fhow-can-i-selectively-escape-percent-in-python-strings%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
26
Why isn't it
%
? That was my guess, I'm surprised to find it's%%
instead - seems pretty counterintuitive.– Demis
Apr 28 '15 at 16:14
2
% i
means "a decimal representation of an integer, padded left with spaces.– Antti Haapala
Apr 2 '16 at 21:19
2
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, andhappens to be the most common escape character, for better or worse.
– shemnon
Oct 14 '16 at 21:00
1
@Demis and how do you escape
\%
? You are bound to require escaping through repetition of special characters, if the special characters are also not special depending on circumstances.– Sassa NF
Jan 17 '17 at 21:48
2
I think it is annoying in Python that the the literal % is encoded by "%%" and not by "%".
– Ralf
Nov 1 '17 at 18:39