There are many ways to do this. To fix your current code using %-formatting, you need to pass in a tuple:
Pass it as a tuple:
print("Total score for %s is %s" % (name, score))
A tuple with a single element looks like ('this',).
Here are some other common ways of doing it:
Pass it as a dictionary:
print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})
There's also new-style string formatting, which might be a little easier to read:
Use new-style string formatting:
print("Total score for {} is {}".format(name, score))Use new-style string formatting with numbers (useful for reordering or printing the same one multiple times):
print("Total score for {0} is {1}".format(name, score))Use new-style string formatting with explicit names:
print("Total score for {n} is {s}".format(n=name, s=score))Concatenate strings:
print("Total score for " + str(name) + " is " + str(score))
The clearest two, in my opinion:
Just pass the values as parameters:
print("Total score for", name, "is", score)If you don't want spaces to be inserted automatically by
printin the above example, change thesepparameter:print("Total score for ", name, " is ", score, sep='')If you're using Python 2, won't be able to use the last two because
printisn't a function in Python 2. You can, however, import this behavior from__future__:from __future__ import print_functionUse the new
f-string formatting in Python 3.6:print(f'Total score for {name} is {score}')
There are many ways to do this. To fix your current code using %-formatting, you need to pass in a tuple:
Pass it as a tuple:
print("Total score for %s is %s" % (name, score))
A tuple with a single element looks like ('this',).
Here are some other common ways of doing it:
Pass it as a dictionary:
print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})
There's also new-style string formatting, which might be a little easier to read:
Use new-style string formatting:
print("Total score for {} is {}".format(name, score))Use new-style string formatting with numbers (useful for reordering or printing the same one multiple times):
print("Total score for {0} is {1}".format(name, score))Use new-style string formatting with explicit names:
print("Total score for {n} is {s}".format(n=name, s=score))Concatenate strings:
print("Total score for " + str(name) + " is " + str(score))
The clearest two, in my opinion:
Just pass the values as parameters:
print("Total score for", name, "is", score)If you don't want spaces to be inserted automatically by
printin the above example, change thesepparameter:print("Total score for ", name, " is ", score, sep='')If you're using Python 2, won't be able to use the last two because
printisn't a function in Python 2. You can, however, import this behavior from__future__:from __future__ import print_functionUse the new
f-string formatting in Python 3.6:print(f'Total score for {name} is {score}')
There are many ways to print that.
Let's have a look with another example.
a = 10
b = 20
c = a + b
#Normal string concatenation
print("sum of", a , "and" , b , "is" , c)
#convert variable into str
print("sum of " + str(a) + " and " + str(b) + " is " + str(c))
# if you want to print in tuple way
print("Sum of %s and %s is %s: " %(a,b,c))
#New style string formatting
print("sum of {} and {} is {}".format(a,b,c))
#in case you want to use repr()
print("sum of " + repr(a) + " and " + repr(b) + " is " + repr(c))
EDIT :
#New f-string formatting from Python 3.6:
print(f'Sum of {a} and {b} is {c}')
I want to print an undefined amount of variables that are named like this: var_1, var_2, var_3, etc.
I created a for loop which runs a specific amount of times depending on how many variables there are:
count = 1
for i in range(x):
print("var_" + str(count))
count += 1
However this prints the variables by their name and not the integer/string they are defined to earlier. I was hoping someone could tell me if there is a way to fix this, or a better way to do it.
I'm currently working on a college assignment and I have to display my first and last name, my birthday, and my lucky number in the format of "My name is {first name} {last name}. I celebrate my birthday on {date of birth}, and my lucky number is {lucky number}!"
Here is what I've cooked up with the like hour of research of down, can anyone help me get it into the format above?
import datetime
x = "Nicholas"
y = "Husnik"
z = str(81)
date_obj = datetime.date(2005, 10, 0o6)
date_str = str(date_obj)
date_str2 = date_obj.strftime("%m/%d/%y")
print("Hello, my name is " + x + " " + y +".")
print("My birthday is on:")
print(date_str2, type(date_str2))
print("My lucky number is " + z + ".")
In python3:
print(string1, string2, sep='\n')
In python2:
print string1 + '\n' + string2
... or from __future__ import print_function and use python3's print
Since my first answer, OP has edited the question with a variable type change. Updating answer for the updated question:
If you have some integers, namely int1 and int2:
Python 3:
print(int1, int2, sep='\n')
Python 2:
print str(int1) + '\n' + str(int2)
or
from __future__ import print_function
print(int1, int2, sep='\n')
or
print '\n'.join([str(i) for i in [int1, int2]])
You can use the new line escape character and concatenate it between the two strings.
print string1 + "\n" + string2
As @Wooble points out, you have a syntax error. But let's clarify how this works in Python.
print(a, b, c, d,....)
is basically the same as
output = " ".join(a, b, c, d)
print(output)
If you want to output the same variable multiple times, sometimes format is better. http://docs.python.org/3/library/string.html#format-string-syntax
The second argument would be arg1 "arg2" which is not a valid expression. If you want to print both arg1 and "arg2", you should separate it by another comma:
print("arg1 =", arg1, "arg2 =", arg2)