TypeError: must be str, not int


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:

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
Advertisements
Try Out Code2care Dev Tools:

Advertisements



#Android #Java #Linux #Microsoft #Google #Python #MacOS #Notepad++ #Microsoft Teams #CSS #PHP #SharePoint #Html #Linux #C-Program #Bootstrap #jQuery #Sublime #Android-Studio #Facebook #Eclipse #WhatsApp #News #MySQL #Json #HowTos #JavaScript #FTP #S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency