5

So I wanna print some text after i print my variables like this:

print('Blablabla' var ' blablabla')

Right now it looks like this:

 print('The enemey gets hit for %d' % damage)

I wanna print the word "Hitpoints" after I've printed the damage variable.

phihag
  • 278,196
  • 72
  • 453
  • 469
user1216862
  • 93
  • 1
  • 1
  • 3
  • 3
    `print('The enemy gets hit for {} hitpoints'.format(damage))`... You could also use `%d`, but `str.format` is the new method. – Gandaro Feb 17 '12 at 18:11
  • 1
    This is a confusing question. It seems hard to understand what's difficult about putting `Hitpoints ` into the format string itself. Can you explain what confused you about the format string? Is the some question about the `%` string operator? – S.Lott Feb 17 '12 at 18:33

3 Answers3

25

Just include the hitpoints:

print('The enemey gets hit for %d hitpoints' % damage)

The formatting operator % is very powerful, have a look at all the placeholder options. It is, however, intended to be phased out in favor of str.format:

print('The enemey gets hit for {} hitpoints'.format(damage))

Alternatively, you can convert the value of damage to a string, and concatenate strings with +:

print('The enemy gets hit for ' + str(damage) + ' hitpoints')
phihag
  • 278,196
  • 72
  • 453
  • 469
  • To concatenate with print, I prefer the stylistic choice of: print('The enemey gets hit for', str(damage), 'hitpoints') – hexparrot Feb 17 '12 at 18:16
  • @hexparrot That works as well, but it requires an understanding of varargs and is not universally portable as it requires `print` to be a function to get the expected output. Also, concatenation is a universal concept, which is useful outside of printing. – phihag Feb 17 '12 at 18:19
  • @phihag Sorry, I am not a native speaker, so I may sound a bit rough sometimes. ;) – Gandaro Feb 17 '12 at 18:19
5

Looks much nicer this way. ;0)

damage = 10
print(f'The enemey gets hit for {damage} hitpoints')

(for python 3.6 & above)

FooBar167
  • 2,721
  • 1
  • 26
  • 37
ruckkar
  • 51
  • 1
  • 3
1

Just add hitpoints to your string:

print('the enemy gets mutilated for %d hitpoints!' % damage)
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
hexparrot
  • 3,399
  • 1
  • 24
  • 33
  • 1
    `%d` is the format specifier for an integer, not fileobject?! http://docs.python.org/library/stdtypes.html#string-formatting – Gandaro Feb 17 '12 at 18:14
  • You're right, I confused %d for fp decimal (%f instead), which is surely not what I intended. – hexparrot Feb 17 '12 at 18:20
  • Well, the python docs call them integer decimals %i and floating-point decimals, which seems to me a confusing naming convention, as integers have neither a decimal point nor decimal values. %d immediately struck me as decimal related (which it is), but just not in the sense I expected. Fileobjects were never part of the solution, you may have confused fp with fo? – hexparrot Feb 17 '12 at 18:27
  • Numbers in base 10 are by definition "decimal values." The part of a number after the decimal point is "the fraction," not "the decimal." Would the portion of a binary number after the binary point be called "the binary"? I think not. – kindall Feb 17 '12 at 18:31
  • 1
    I feel like the mistake has now been appropriately addressed, acknowledged, and remedied. I don't believe this much explanation was necessary for a simple confusion that 'd' was the same as 'i', and not the 'f' I thought it meant. – hexparrot Feb 17 '12 at 18:37