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.
How to print a variable and a string in Python by separating each with a comma. You can print text alongside a variable, separated by commas, in one print statement.
Using string concatenation: String concatenation is a method that can be used to add strings together. This is done by using the “+” character between two variables or strings. This way we can use Python to print variable values along with the string.
Use a formatted string literal to print a variable's name and value, e.g. print(f'{variable=}') . You can use an expression in f-strings to get a string that contains the variable's name and value.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With