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.
How to print a variable and a string at once in Python 3
How can I print variable and string on same line in Python? - Stack Overflow
why is print a legal variable name?
When I want to print the value of a variable, is there any good way to also print its identifier?
How to print multiple variables in Python?
What are the different methods to print variables in Python?
How to print a variable and a string in Python using concatenation?
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?
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