TypeError: must be str, not int [Fix Python]


TypeError is the most common error that you may see while working with Python code. Let's see an example,

import time

n = 10;
print("Sleeping for " + n + " seconds..")
time.sleep(n)
print("Done!")

When you try executing the above code you will get such a stack trace,

------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-9d9f3e4b3530> in <module>()
      2 
      3 n = 10;
----> 4 print("Sleeping for " + n + " seconds..")
      5 time.sleep(n)
      6 print("Done!")

TypeError: must be str, not int

Solution/Fix:

You need to cast the integer data type String using the str() function.

print("Sleeping for " + str(n) + " seconds..")
TypeError - must be str not int

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap