3

Which string implementation in python is more pythonic?

string += "<%s>%s</%s>%s" % (a, b, c, d)

or

string += '<' + a + '>' + b + '<'/' + c + '>' + d
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
asked Mar 5, 2014 at 20:20

1 Answer 1

8

String formatting is faster and more readable, always.

Consider using named parameters, and the more recent str.format() method:

"<{tag}>{value}</{tag}>{tail}".format(tag='foo', value='bar', tail='spam')
answered Mar 5, 2014 at 20:22
Sign up to request clarification or add additional context in comments.

1 Comment

That function str.format() looks great for my script. Thank you

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.