Console wants print(stuff) how do I use %?


#1



String Formatting with %, Part 1

As I am learning with this site, I also have a Python 3.6.0 shell open, and I practice concepts as I go. This lesson is giving me some trouble due to the difference in the way "print" works. On this site, if myVariable = "cat", print myVariable retuns cat. On the 3.6.0 console, I have to use print(myVariable)

I tried both of these, neither works:


string1 = "hull"
string2 = 99
string3 = "breach"

print("If you're having %s problems I feel bad for you son, I got %s problems but a %s aint one.") % (string1,string2,string3)

print("If you're having %s problems I feel bad for you son, I got %s problems but a %s aint one." % (string1,string2,string3))


#2

You still have a parenthesis on your second print line.


#3

According to pyformat.info, you would use print("{0} had a little {1}".format('Mary','lamb')).

Applied to your example:

string1 = "hull"
string2 = 99
string3 = "breach"

print("If you're having {0} problems I feel bad for you son, I got {1} problems but a {2} aint one.".format(string1,string2,string3))

#4

Thanks Ricky, that works in the console.

So why are they different? Which one should I practice?


#5

Intentionally. I was trying to see if the end parenthesis position was the issue.


#6

https://wiki.python.org/moin/Python2orPython3

Short version: Python 2.x is legacy, Python 3.x is the present and future of the language


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.