Can I Print the Same Variable Twice?


#1

If i wanted to use the variable twice in the same string, is that possible?

eg.

string_1 = “Camelot”
print “Let’s not go to %s. 'Tis a silly %s.” % (string_1)

How do I make this output:
Let’s not go to Camelot. 'Tis a silly Camelot

I know the sentence doesn’t make sense but is it possible to do that.


FAQ: Learn Python – Strings & Console Output – String Formatting with %, Part 1
#2

Hi @amansingh3662241980,

You can use the same variable twice, but you need to pass it in each time you use it. So your example would look like this:

string_1 = "Camelot"
print "Let’s not go to %s. 'Tis a silly %s." % (string_1, string_1)

Or, in Python 3:

print("Let’s not go to {}. 'Tis a silly {}.".format(string_1, string_1))