print variable and a string in python
Solution 1
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.
Solution 2
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.
Solution 3
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.
Solution 4
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.
Solution 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
user203558
Updated on August 24, 2021Comments
-
user203558 almost 3 years
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)
. -
s1lence over 11 yearswhat about the possibility of format strings and basic string concatenation? Could extend your answer in a nice way showing different approaches for different purposes.
-
alexvassel over 11 years@Martijn Pieters: I was nanosecond first with
format
answer) -
Martijn Pieters over 11 years@alexvassel: But mine contains documentation links, a better example, and points to
%
string formatting too. :-P -
alexvassel over 11 years@Martijn Pieters: True things for sure) Deleting my answer.
-
user203558 over 11 yearsIs it okay even if the result is not string but a number??
-
yantrab over 11 years"semi-deprecated": it isn't deprecated, semi- or otherwise.
-
Martijn Pieters over 11 years@NedBatchelder: There has been deprecation 'noise' about it, I know it's not deprecated now but it is clear
.format()
is to be preferred. -
Ignacio Ara about 6 yearsPlease explain your lines of code so other users can understand its functionality
-
jww about 5 yearsScrew F-strings. They break on both Python2 and Python3. The Python devs should get a Darwin award for breaking basic I/O yet again.
-
Vignesh Krishnan about 5 yearsThis 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().
-
Admin about 5 yearsWorks fine :) for a multiple var injection (with outfile print ) : print("SELECT mot FROM " + choixL + " WHERE mot LIKE " + mot + ";", file=open("verif.sql", "a"))
-
forresthopkinsa about 5 years@NuX_o Be careful not to let any user input into those fields!
-
tripleee almost 3 yearsThis only works if the variable you want to interpolate is a string, though.
-
tripleee almost 3 yearsNothing here is specific to Python 3.6.1, though the output will look weird in Python 2.