1

I'm building a string via the following:

pagination +='<li>''<a href="/main/?page=%(current_link)s'+link+'">%(current)s</a></li>' % \
                     {'current_link': current_link, 'current': current_link}

When viewing the page in the browser, the text shows up fine but the link points to "..%(current_link)s...". I've tried naming both of them 'current_link' in the string itself but that didn't work so I tried the approach above.

Any suggestions?

3 Answers 3

3

The problem is you have separated the string, making the % operator only work on the last part.

Try it like this:

pagination +='<li><a href="/main/?page=%(current_link)s>%(current)s</a></li>' % \
                     {'current_link': current_link, 'current': current_link}

Or if you intended the link variable in there like so:

pagination +='<li><a href="/main/?page=%(current_link)s%(link)s>%(current)s</a></li>' % \
                     {'link': link, 'current_link': current_link, 'current': current_link}
Sign up to request clarification or add additional context in comments.

4 Comments

Okay thanks I'll try that. How did you get the HTML to show up in the question though? I was about to delete the question because I couldn't get it to show up
@user1100778, if you start a line with four spaces it will show up as code. Read here for other formatting help: stackoverflow.com/editing-help
Okay I tried it out and it works great and makes sense. Thanks everyone!
@user1100778, you can mark the question as solved if it helped you :)
1

Hm. You are mixing concatenation with + and formatting with %, and I think, this is a matter of operator precedence: % binds stronger than +, so

("%(a)s" + "%(b)s" % { 'a': 'A', 'b': 'B' }) == '%(a)sB'

Comments

0

Don't combine cramming strings together with no operator ('foo''bar'), concatenating with +, and formatting with %. You're only formatting the final string.

Comments

Your Answer

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.