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.
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.
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.
What is the easiest way to print text and a variable together?
Can you print a string and an integer with + in Python?
How do you print without a newline in Python?
Use , to separate strings and variables while printing:
print("If there was a birth every 7 seconds, there would be: ", births, "births")
, in print function separates the items by a single space:
>>> print("foo", "bar", "spam")
foo bar spam
or better use string formatting:
print("If there was a birth every 7 seconds, there would be: {} births".format(births))
String formatting is much more powerful and allows you to do some other things as well, like padding, fill, alignment, width, set precision, etc.
>>> print("{:d} {:03d} {:>20f}".format(1, 2, 1.1))
1 002 1.100000
^^^
0's padded to 2
Demo:
>>> births = 4
>>> print("If there was a birth every 7 seconds, there would be: ", births, "births")
If there was a birth every 7 seconds, there would be: 4 births
# formatting
>>> print("If there was a birth every 7 seconds, there would be: {} births".format(births))
If there was a birth every 7 seconds, there would be: 4 births
Python is a very versatile language. You may print variables by different methods. I have listed below five methods. You may use them according to your convenience.
Example:
a = 1
b = 'ball'
Method 1:
print('I have %d %s' % (a, b))
Method 2:
print('I have', a, b)
Method 3:
print('I have {} {}'.format(a, b))
Method 4:
print('I have ' + str(a) + ' ' + b)
Method 5:
print(f'I have {a} {b}')
The output would be:
I have 1 ball
Fancier, do:
print(f'Hello my name is {name}.')
There are multiple ways you can do that
# f-strings
print(f"Hello, my name is: {name}")
# Str.format()
print("Hello, my name is: {name}".format(name))
# Concatenation
print("Hello, my name is: " + name)
# Simple print
print("Hello, my name is: ", name)
You can choose whichever one you prefer the most.
In the title how do I print both a static string and a varaible at once, I tried doing it like this:
print ("The year of birth for ") (person)
But it gave me a notype error. But that makes no sense to me as this program has no functions and the variable was defined and worked fine earlier in the program. Can someone help?