Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python printing text after printing a variables

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.

like image 769
user1216862 Avatar asked Feb 17 '12 18:02

user1216862


People also ask

How do you print both variables and text in Python?

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.

How do you print a variable value in print statement in Python?

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.

How do you print variable names and values in Python?

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.


1 Answers

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')
like image 141
phihag Avatar answered Sep 23 '22 00:09

phihag