77

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)
1
  • 1
    The syntax in the question will only work in Python 2, where print was not yet a function. Most of the answers here will work in both Python 2 and Python 3; anything where print is not followed by a left round parenthesis ( is Python 2 only, whereas the ones with parentheses will generally work in both versions. Commented Aug 23, 2021 at 15:31

7 Answers 7

69

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.

Sign up to request clarification or add additional context in comments.

3 Comments

what about the possibility of format strings and basic string concatenation? Could extend your answer in a nice way showing different approaches for different purposes.
"semi-deprecated": it isn't deprecated, semi- or otherwise.
@NedBatchelder: There has been deprecation 'noise' about it, I know it's not deprecated now but it is clear .format() is to be preferred.
48

If you are using python 3.6 and newer then you can use f-strings to do the task like this.

print(f"I have {card.price}")

just include f in front of your string and add the variable inside curly braces { }.

Refer to a blog The new f-strings in Python 3.6: written by Christoph Zwerschke which includes execution times of the various method.

2 Comments

Screw F-strings. They break on both Python2 and Python3. The Python devs should get a Darwin award for breaking basic I/O yet again.
This feature was introduced in python 3.6. it would obviously break if you use it on earlier python releases. don't use them if you are using legacy python. you can use the good old format().
39

Something that (surprisingly) hasn't been mentioned here is simple concatenation.

Example:

foo = "seven"

print("She lives with " + foo + " small men")

Result:

She lives with seven small men

Additionally, as of Python 3, the % method is deprecated. Don't use that.

3 Comments

Works fine :) for a multiple var injection (with outfile print ) : print("SELECT mot FROM " + choixL + " WHERE mot LIKE " + mot + ";", file=open("verif.sql", "a"))
@NuX_o Be careful not to let any user input into those fields!
This only works if the variable you want to interpolate is a string, though.
11

Assuming you use Python 2.7 (not 3):

print "I have", card.price (as mentioned above).

print "I have %s" % card.price (using string formatting)

print " ".join(map(str, ["I have", card.price])) (by joining lists)

There are a lot of ways to do the same, actually. I would prefer the second one.

1 Comment

Is it okay even if the result is not string but a number??
5

From what I know, printing can be done in many ways

Here's what I follow:

Printing string with variables

a = 1
b = "ball"
print("I have", a, b)

Versus printing string with functions

a = 1
b = "ball"
print("I have" + str(a) + str(b))

In this case, str() is a function that takes a variable and spits out what its assigned to as a string

They both yield the same print, but in two different ways. I hope that was helpful

Comments

3

If the Python version you installed is 3.6.1, you can print strings and a variable through a single line of code. For example the first string is "I have", the second string is "US Dollars" and the variable `card.pricè is equal to 300, we can write the code this way:

print("I have", card.price, "US Dollars")

The print() function outputs strings to the screen.
The comma lets you concatenate and print strings and variables together in a single line of code.

2 Comments

Please explain your lines of code so other users can understand its functionality
Nothing here is specific to Python 3.6.1, though the output will look weird in Python 2.
1

Well you could use f-strings for concatenation of int and str or for concatenation of any data type.

Here is an example of f-strings in my code below.

Hope it helps!

name = "abc"
print(f"Your name is {name}")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.