Which of these Python string interpolations is proper (not a trick question)?
'%s' % my_string'%s' % (my_string)'%s' % (my_string, )
If it varies by version, please summarize.
Which of these Python string interpolations is proper (not a trick question)?
'%s' % my_string'%s' % (my_string)'%s' % (my_string, )If it varies by version, please summarize.
The first one is the most common one. The third has unnecessary parenthesis and doesn't help the legibility if you've only one object you want to use in your format-string. The second is just plain silly, because that's not even a tuple.
Nowadays starting with Python 2.6, there's a new and recommended way of formatting strings using the .format-method:
In your case you would use:
'{}'.format(my_string)
The advantage of the new format-syntax are that you enter more advanced formattings in the format string.
format is in Python 2.6+All three of these are equivalent.
The first two are exactly equivalent, in fact. Putting brackets around something does not make it a tuple: putting a comma after it does that. So the second one evaluates to the first.
The third is also valid: using a tuple is the normal way of doing string substitution, but as a special case Python allows a single value if there is only one element to be substituted.