Unlike countless other programming languages out there, Python does no implicitly typecast integers (or floats) to strings as soon as you concatenate them come strings.

You are watching: Typeerror: must be str, not int

Fortunately, Python has a handy built-in role str() which will convert the discussion passed in come a wire format.

The Wrong method to convert a String to an essence in Python

Programmers comes from other programming languages might attempt to do the adhering to string concatenation, i beg your pardon will develop an error:

age = 18string = "Hello, i am " + age + " year old"You deserve to run this code on repl.it.

The error that shows up is:

Traceback (most recent call last): record "python", line 3, in TypeError: have to be str, no intHere, TypeError: have to be str, not int shows that the creature must an initial be converted to a string prior to it deserve to be concatenated.

The Correct way to convert a String come an integer in Python

Here"s a basic concatenation example:

age = 18print("Hello, i am " + str(age) + " years old")# Output# Hello, ns am 18 year oldYou can run this code on repl.it.

Here"s exactly how to print 1 2 3 4 5 6 7 8 9 10 utilizing a single string:

result = ""for i in range(1, 11): result += str(i) + " "print(result)# Output# 1 2 3 4 5 6 7 8 9 10You have the right to run the password on repl.it.

Here"s a line-by-Line explanation of just how the over code works:

First of every a change ‘result’ is assigned come an empty string.The for loop is being offered to iterate over a list of numbers.This perform of number is created using the selection function.so range(1,11) is going to create a perform of numbers from 1 to 10.On each because that loop iteration this ‘i’ change is walking to take it up values from 1 come 10.On an initial iteration once the change i=1,then the variable ,str(i) counter the ‘i’ i beg your pardon is one integer worth to a wire value.Since i=1, on the an initial iteration finally result=1.And the same procedure goes on till i=10 and finally ~ the critical iteration result=1 2 3 4 5 6 7 8 9 10.Therefore as soon as we lastly print the an outcome after the because that loop the output on the console is ‘1 2 3 4 5 6 7 8 9 10’.

I expect you"ve uncovered this helpful. Happy coding.

See more: Which Of The Following Statements Best Describes The Anti-Federalist View Of The Constitution?


*
chris Tse

I'm a software application engineer in ~ Oracle Netsuite doing former end development with React and Sass. I likewise enjoy developing fast, responsive sites through the JAMStack.


find out to password for free. Tastecraftedmcd.com's open resource curriculum has actually helped more than 40,000 people get tasks as developers. Acquire started


tastecraftedmcd.com is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United says Federal taxes Identification Number: 82-0779546)

ours mission: to aid people learn to code for free. We attain this by developing thousands the videos, articles, and interactive coding class - every freely accessible to the public. We also have countless tastecraftedmcd.com examine groups about the world.

Donations to tastecraftedmcd.com go towards our education initiatives and assist pay because that servers, services, and also staff.