Friday, 15 August 2014

string - Python 3: using %s and .format() -



string - Python 3: using %s and .format() -

i have switched % .format() string formatting operator in 2.x code in order create easier migrate 3.x in future. bit surprising find out not %-style formatting remains in py3, used in standard library code. seems logical, because writing '(%s)' % variable bit shorter , maybe easier comprehend '({})'.format(variable). i'm still in doubt. proper (pythonic?) utilize both approaches in code? give thanks you.

python 3.2 documentation said that, % go away.

http://docs.python.org/3.2/tutorial/inputoutput.html#old-string-formatting

since str.format() quite new, lot of python code still uses % operator. however, because old style of formatting removed language, str.format() should used.

but @regilero says, the sentence gone 3.3, might suggest it's not case. there conversations here suggest same thing.

as of python 3.4 paragraph 7.1.1 reads:

the % operator can used string formatting. interprets left argument much sprintf()-style format string applied right argument, , returns string resulting formatting operation.

see python 3.4 4.7.2 printf-style string formatting.

python string string-formatting

No comments:

Post a Comment