I have the following string in python, which is then rendered in a template with Django.
link_string = '<a href="/search/?q=%23%s"> %s </a>'
It is a link to a search url where %23 is '#', because I'm trying to search objects with #hashtag.
I intend to interpolate this string with 2 values, %s after the %23 and another one:
link_href = link_string % ('hashtag_value', 'Link Name')
What would be the proper way to obtain the following final string:
<a href="/search/?q=%23hashtag_value"> Link Name </a>
Thank you very much
asked Feb 13, 2013 at 19:45
PepperoniPizza
9,18211 gold badges68 silver badges110 bronze badges
-
1Do you absolutely have to urlencode the pound sign?Makoto– Makoto2013年02月13日 19:46:29 +00:00Commented Feb 13, 2013 at 19:46
3 Answers 3
link_string = '<a href="/search/?q=%%23%s"> %s </a>'
answered Feb 13, 2013 at 19:46
Pavel Anossov
63.4k16 gold badges156 silver badges125 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
% is escaped using double percent %%.
answered Feb 13, 2013 at 19:47
Ivaylo Strandjev
71.4k18 gold badges130 silver badges183 bronze badges
Comments
Just type % character twice
link_string = '<a href="/search/?q=%%23%s"> %s </a>'
answered Feb 13, 2013 at 19:47
bsiamionau
8,2555 gold badges52 silver badges74 bronze badges
Comments
Explore related questions
See similar questions with these tags.
lang-py