TypeError: unsupported operand type(s) for %: 'NoneType' and 'str' in Python

Dung Do Tien Sep 07 2021 194

Hello you guys, I am a newbie in Python and I'm also studying more about Python.

I have a small assignment, I want manipulation with a string text and concat it. Like this:

x = "Covid - 19 is comming and kill over %d peoples." % 3000000
totalcase = "400M"
covercase = "350M"
y = "total case is %s and total cover case is %s" % (totalcase, covercase)

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

But I get an exception TypeError: unsupported operand type(s) for %: 'NoneType' and 'str' when I run code above.

Covid - 19 is comming and kill over 3000000 peoples.
total case is 400M and total cover case is 350M
I feel: %r
Traceback (most recent call last):
  File "main.py", line 8, in <module>
    print("I feel: %r") % x
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

And I am using python 3.8.2

Anyone can explain it to me? How can I solve it?

Thanks for any response.

Have 1 answer(s) found.
  • A

    Anand Pissey Sep 07 2021

    I think you got problem with line code below :

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

    you need to know that print() method it returns None value.

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

    print("I feel: {!r}".format(x))

    And it worked for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close