print variable and a string in python

487,370

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

Share:
487,370
user203558
Author by

user203558

Updated on August 24, 2021

Comments

  • user203558
    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
    s1lence over 11 years
    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.
  • alexvassel
    alexvassel over 11 years
    @Martijn Pieters: I was nanosecond first with format answer)
  • Martijn Pieters
    Martijn Pieters over 11 years
    @alexvassel: But mine contains documentation links, a better example, and points to % string formatting too. :-P
  • alexvassel
    alexvassel over 11 years
    @Martijn Pieters: True things for sure) Deleting my answer.
  • user203558
    user203558 over 11 years
    Is it okay even if the result is not string but a number??
  • yantrab
    yantrab over 11 years
    "semi-deprecated": it isn't deprecated, semi- or otherwise.
  • Martijn Pieters
    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
    Ignacio Ara about 6 years
    Please explain your lines of code so other users can understand its functionality
  • jww
    jww about 5 years
    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.
  • Vignesh Krishnan
    Vignesh Krishnan about 5 years
    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().
  • Admin
    Admin about 5 years
    Works fine :) for a multiple var injection (with outfile print ) : print("SELECT mot FROM " + choixL + " WHERE mot LIKE " + mot + ";", file=open("verif.sql", "a"))
  • forresthopkinsa
    forresthopkinsa about 5 years
    @NuX_o Be careful not to let any user input into those fields!
  • tripleee
    tripleee almost 3 years
    This only works if the variable you want to interpolate is a string, though.
  • tripleee
    tripleee almost 3 years
    Nothing here is specific to Python 3.6.1, though the output will look weird in Python 2.