Back
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).
To print variable and a string in python you should do something as follows:-
Example:
foo = "seven" print("abc " + foo + "def")
foo = "seven"
print("abc " + foo + "def")
To know more about this you can have a look at the following video tutorial:-
You can print multiple values separated by a comma:
print "I have", card.price
Here, the print statement will output each expression separated by spaces, followed by a newline. But, If you need more complex formatting, then you can use the ''.format() method like:
print "I have: {0.price}".format(card)
Or by using the older and semi-deprecated % string formatting operator.
For more information you can refer to these links:
https://docs.python.org/2/reference/simple_stmts.html#the-print-statement
https://docs.python.org/2/library/stdtypes.html#string-formatting-operations
https://docs.python.org/2/library/stdtypes.html#str.format
31k questions
32.8k answers
501 comments
693 users