I'm probably missing something here, after searching I couldn't find an answer.
I've explored quite a few Python projects and one thing I keep noticing is the fact that the majority of them continue to use the %
operator for formatting strings rather than the newer, recommended .format()
method. Is there a reason for this? it seems like a trivial change, unless I'm missing something entirely.
For example:
# count how many times the % operator technique is used
find . -name "*.py" -exec grep -HE "\"[^\"]+\"\s\%\s\w+|'[^']+'\s\%\s\w+" {} \; | wc -l
# and the same for format()
find . -name "*.py" -exec grep -HE "\w+\.format\(" {} \; | wc -l
# Results:
#
# % operator format()
# iPython 670 63
# Django 977 8
# Tornado 91 0
# requests 25 1
No real reason for this question, just curious.
Cheers guys!
.format()
method?%
is more efficient, and less verbose for simple string replacements. I use it most of times, and when I need more powerful formatting I go for.format