Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print variable and a string in python

Alright, I know how to print variables and strings. But how can I print something like "My string" card.price (it is my variable). I mean, here is my code: print "I have " (and here I would like to print my variable card.price).

like image 965
user203558 Avatar asked Dec 26 '12 14:12

user203558


People also ask

How do you print variable names and values in Python?

Using f-strings in Python to print variables is the most commonly used method and I would personally recommend using this method. In this method, an 'f' is placed before the opening quotation mark of a string. Braces {} are placed around the names of variables that you are looking to print.


1 Answers

By printing multiple values separated by a comma:

print "I have", card.price 

The print statement will output each expression separated by spaces, followed by a newline.

If you need more complex formatting, use the ''.format() method:

print "I have: {0.price}".format(card) 

or by using the older and semi-deprecated % string formatting operator.

like image 96
Martijn Pieters Avatar answered Sep 28 '22 09:09

Martijn Pieters