0

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
1
  • 1
    Do you absolutely have to urlencode the pound sign? Commented Feb 13, 2013 at 19:46

3 Answers 3

3
link_string = '<a href="/search/?q=%%23%s"> %s </a>'
answered Feb 13, 2013 at 19:46
Sign up to request clarification or add additional context in comments.

Comments

3

% is escaped using double percent %%.

answered Feb 13, 2013 at 19:47

Comments

1

Just type % character twice

link_string = '<a href="/search/?q=%%23%s"> %s </a>'
answered Feb 13, 2013 at 19:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.