Is it more Pythonic to use String Formatting over String Concatenation in Python 3?

青春壹個敷衍的年華 提交于 2019-11-29 08:54:24
Eddie

Depends upon how long your string is and how many variables. For your use case I believe string.format is better as it has a better performance and looks cleaner to read.

Sometimes for longer strings + looks cleaner because the position of the variables are preserved where they should be in the string and you don't have to move your eyes around to map the position of {} to the corresponding variable.

If you can manage to upgrade to Python 3.6 you can use the newer more intuitive string formatting syntax like below and have best of both worlds:

player = 'Arbiter'
health = 100
print(f'{player} has {health} health left.')

If you have a very large string, I recommend to use a template engine like Jinja2 (http://jinja.pocoo.org/docs/dev/) or something along the line.

Ref: https://www.python.org/dev/peps/pep-0498/

format() is better:

  1. Better, performance-wise.
  2. clearer. You can see how the sentence looks like and what are the parameters, you don't have a bunch of + and ' all around.
  3. Gives you more features for example how many places after zero in a floating point, thus more flexible to changes.

It depends on the quantity and type of objects you're combining, as well as the kind of output you want.

>>> d = '20160105'
>>> t = '013640'
>>> d+t
'20160105013640'
>>> '{}{}'.format(d, t)
'20160105013640'
>>> hundreds = 2
>>> fifties = 1
>>> twenties = 1
>>> tens = 1
>>> fives = 1
>>> ones = 1
>>> quarters = 2
>>> dimes = 1
>>> nickels = 1
>>> pennies = 1
>>> 'I have ' + str(hundreds) + ' hundreds, ' + str(fifties) + ' fifties, ' + str(twenties) + ' twenties, ' + str(tens) + ' tens, ' + str(fives) + ' fives, ' + str(ones) + ' ones, ' + str(quarters) + ' quarters, ' + str(dimes) + ' dimes, ' + str(nickels) + ' nickels, and ' + str(pennies) + ' pennies.'
'I have 2 hundreds, 1 fifties, 1 twenties, 1 tens, 1 fives, 1 ones, 2 quarters, 1 dimes, 1 nickels, and 1 pennies.'
>>> 'I have {} hundreds, {} fifties, {} twenties, {} tens, {} fives, {} ones, {} quarters, {} dimes, {} nickels, and {} pennies.'.format(hundreds, fifties, twenties, tens, fives, ones, quarters, dimes, nickels, pennies)
'I have 2 hundreds, 1 fifties, 1 twenties, 1 tens, 1 fives, 1 ones, 2 quarters, 1 dimes, 1 nickels, and 1 pennies.'
>>> f'I have {hundreds} hundreds, {fifties} fifties, {twenties} twenties, {tens} tens, {fives} fives, {ones} ones, {quarters} quarters, {dimes} dimes, {nickels} nickels, and {pennies} pennies.'
'I have 2 hundreds, 1 fifties, 1 twenties, 1 tens, 1 fives, 1 ones, 2 quarters, 1 dimes, 1 nickels, and 1 pennies.'

It is much easier to create without error a large format string than it is to do a lot of concatenation, too. Add in the fact that format strings can handle actual formatting, like alignment or rounding, and you'll soon leave concatenation for only the simplest cases, as shown above.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!