Unlike many other programming languages out there, Python does not implicitly typecast integers (or floats) to strings when you concatenate them to strings.

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

Fortunately, Python has a handy built-in function str() which will convert the argument passed in to a string format.

The Wrong Way to Convert a String to an Integer in Python

Programmers coming from other programming languages may attempt to do the following string concatenation, which will produce an error:

age = 18string = "Hello, I am " + age + " years old"You can run this code on repl.it.

The error that shows up is:

Traceback (most recent call last): File "python", line 3, in TypeError: must be str, not intHere, TypeError: must be str, not int indicates that the integer must first be converted to a string before it can be concatenated.

The Correct Way to Convert a String to an Integer in Python

Here"s a simple concatenation example:

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

Here"s how to print 1 2 3 4 5 6 7 8 9 10 using 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 can run the code on repl.it.

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

First of all a variable ‘result’ is assigned to an empty string.The for loop is being used to iterate over a list of numbers.This list of numbers is generated using the range function.so range(1,11) is going to generate a list of numbers from 1 to 10.On each for loop iteration this ‘i’ variable is going to take up values from 1 to 10.On first iteration when the variable i=1,then the variable ,str(i) converts the ‘i’ which is an integer value to a string value.Since i=1, on the first iteration finally result=1.And the same process goes on until i=10 and finally after the last iteration result=1 2 3 4 5 6 7 8 9 10.Therefore when we finally print the result after the for loop the output on the console is ‘1 2 3 4 5 6 7 8 9 10’.

I hope you"ve found 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 engineer at Oracle Netsuite doing front end development with React and Sass. I also enjoy creating fast, responsive sites with the JAMStack.


Learn to code for free. tastecraftedmcd.com's open source curriculum has helped more than 40,000 people get jobs as developers. Get started


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

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons - all freely available to the public. We also have thousands of tastecraftedmcd.com study groups around the world.

Donations to tastecraftedmcd.com go toward our education initiatives and help pay for servers, services, and staff.