Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

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).

2 Answers

0 votes
by (106k points)
edited by

To print variable and a string in python you should do something as follows:-

Example:

foo = "seven" 

print("abc " + foo + "def")

To know more about this you can have a look at the following video tutorial:-

0 votes
by (20.3k points)

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

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 3, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...