Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to convert int to string in Python

Understanding Data Types

As you're learning programming, it's like learning a new language. Just like in English we have nouns, verbs, adjectives, in Python, we have integers (int), strings (str), and many more. These are called data types, and they are like the building blocks of the language.

An integer, as you might remember from math class, is a whole number, without a fraction or decimal part. In Python, we represent integers as int. For example, 5, 0, -2 are all integers.

A string, on the other hand, is a sequence of characters. It can include letters, numerals, symbols, and even spaces. In Python, we represent strings as str. For example, 'Hello', '123', '   ' are all strings.

Converting int to str in Python

Now, let's say we have an integer and we want to convert it into a string. Why would we want to do that? Well, let's say you're writing a program to send out birthday invitations. You have the age of the person as an integer, but you want to include it in the text of the invitation, which is a string.

You can convert an integer to a string in Python using the str() function. Here's how it works:

age = 25
age_as_string = str(age)
print(age_as_string)

When you run this code, the output will be '25'. Notice the quotes around the number. That's how Python indicates that 25 is a string, not an integer.

Breaking Down the Code

So what's happening in the code above? It's like a recipe.

First, we're setting the variable age to the integer 25. You can think of a variable as a box where you can store things. In this case, we're storing the number 25 in the box labeled age.

Next, we're using the str() function to convert the integer 25 into a string. It's like we're taking the number out of the box, painting it a different color, and then putting it back in a new box labeled age_as_string.

Finally, we're using the print() function to display the contents of the age_as_string box. When we look inside the box, we see '25', which is a string.

When to Use str()

The str() function is like a magic wand that can convert almost any data type into a string. But remember, with great power comes great responsibility. While it can be handy to convert integers to strings, it's not always the best solution.

For example, if you want to perform mathematical operations on a number, you should keep it as an integer. You can't add or subtract strings, or do any of the other mathematical operations you can do with integers.

Here's an example of what happens when you try to add two strings:

first_number = '5'
second_number = '3'
sum = first_number + second_number
print(sum)

The output of this code will be '53', not 8. That's because Python treats the + operator as a concatenation operator when used with strings. Concatenation is like gluing two strings together. So '5' + '3' becomes '53'.

Conclusion

Python, like any other programming language, has its own types of data and ways to manipulate and convert them. As a new programmer, understanding these data types and their conversion methods is akin to learning the alphabet of a new language. It's fundamental!

We've embarked on a journey together, exploring the vast territories of integers and strings, and learning how to transform one into the other. Like a chameleon changing its colors, or a caterpillar morphing into a butterfly, we've seen how the str() function can metamorphose an integer into a string.

Remember, programming is not just about writing code, but also about solving problems and implementing solutions in a creative and efficient way. So, the next time you find yourself needing to convert an integer to a string in python, you can simply call upon the str() function, your newly acquired magical wand, and voila!

Happy coding!