Is more Pythonic using string formatting by string concatenation in Python 3?

So, I program a text game in Python 3.4 , which very often requires the use of the print() function to display variables to the user.

Two ways I've always done this are string formatting and string concatenation :

 print('{} has {} health left.'.format(player, health)) 

and

 print(player + ' has ' + str(health) + ' health left.') 

So which is better? They are equally readable and quickly printed, and do exactly the same thing. Which one is more Pythonic and why?

The question was asked because I could not find the answer for this in Stack Overflow, which was not related to Java.

+1
source share
3 answers

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

Sometimes, for longer lines, + looks cleaner because the position of the variables is kept where they should be in the line, and you do not need to move your eyes to match the position of {} with the corresponding variable.

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

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

If you have a very large line, I recommend using a template engine like Jinja2 ( http://jinja.pocoo.org/docs/dev/ ) or something else line.

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

+4
source

format() better:

  • Better performance.
  • more understandable. You can see what the offer looks like and what the parameters are, you do not have a bunch of + and "around".
  • It gives you more options, for example, how many places after zero in a floating point, thereby more flexible for changes.
+4
source

It depends on the number and type of objects that you combine, as well as the type of result 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 a large format string without errors than to do a lot of concatenation. Add in the fact that format strings can handle actual formatting, such as alignment or rounding, and you will soon leave concatenation only for the simplest cases, as shown above.

+3
source

All Articles