Can I Print the Same Variable Twice?

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.

13 Likes

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))
16 Likes

Thanks so much for the helpful explanation!
My curiosity was piqued when I saw your answer in the python 3 format, which makes me wonder with version of python your first answer was in regards to. Let me know if this makes sense.

2 Likes

@saums The first example I gave is for Python 2 (2.7).

2 Likes

Thank you! I tried to do what @amansingh3662241980 was trying to do and got an error, but now know how to do it, thanks to your explanation!