28

So I am VERY new to programming and I started with Python 3. I started reading "Learn Python the Hard Way". Now, I got to a point where I had this code:

x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s" % (binary, do_not)

print(x)
print(y)
print("I said: %r") % x

I do not really know the difference between %r, %s and %d. The error I get is TypeError: unsupported operand type(s) for %: 'NoneType' and 'str' No idea what to do and how to fix it. Please explain how I can actually make it work and why it won't work. Also, what is the difference between %r,d and s? Any useful links? Thank you in advance.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
user3586591
  • 289
  • 1
  • 3
  • 6
  • 13
    why the downvote? this is a new user, let's not chase him/her away without an explanation. – markgiaconia Apr 29 '14 at 18:48
  • @markg I guess because the question is very poor and it is trivial to find this kind of thing out. – juanchopanza Apr 29 '14 at 18:49
  • @markg because the error tells the OP precisely what the problem is? – jonrsharpe Apr 29 '14 at 18:49
  • I find the new style formatting more readable and intuitive. `'I said {}'.format(x)`. Then you can do, for example, `{:.2f}` to print a number rounded to two decimal places. – Davidmh Apr 29 '14 at 18:50
  • 1
    @jonrsharpe it does, if you know Python. For a very beginner, it is not so easy to read (but probably achievable, if one bangs their heads against it enough time). – Davidmh Apr 29 '14 at 18:52
  • @juanchopanza why can't this be the place they find it out? – markgiaconia Apr 29 '14 at 18:52
  • @markg Because this is not the kind of place for that. http://stackoverflow.com/questions/how-to-ask – juanchopanza Apr 29 '14 at 18:53
  • @jonrsharpe the error is only intuitive if you have experience interperting it – markgiaconia Apr 29 '14 at 18:57
  • 6
    this reminds me of when i first joined this platform, a team of well of 12 will visit my question and mark it as unconstructive, un-this un-that and leave. no direction, no assistance, no explanation. so i stopped asking. – Ajayi Oluwaseun Emmanuel Aug 12 '15 at 13:29

2 Answers2

20

You want to apply % to the string instead:

print("I said: %r" % x)

Your code is applying it to the return value of the print() call, which returns None.

Alternatively, you can switch to using str.format():

print("I said: {!r}".format(x))
Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
18

You are calling the % outside of the print() function. This tries to see if the actual function print can be printed as %r, and because print doesn't return anything, it tries to get %r for the value None (hence the NoneType error). Change it to:

print("I said: %r" %(x))

The following code:

#!/usr/local/bin/python3
x = "Hello"
print ("Hello World! %s") %(x)

Raises the following error:

Hello World! %s
Traceback (most recent call last):
  File "main.py", line 3, in 
    print ("Hello World! %s") %(x)
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

Changing the code to the following works:

#!/usr/local/bin/python3
x = "Hello"
print ("Hello World! %s" %(x))

Output:

Hello World! Hello
A.J. Uppal
  • 17,608
  • 6
  • 40
  • 70