Copy>>> str(42)
'42'

>>> int('42')
42

Links to the documentation:

  • int()
  • str()

str(x) converts any object x to a string by calling x.__str__(), or repr(x) if x doesn't have a __str__() method.

Answer from Bastien Léonard on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-integer-to-string-in-python
Convert Integer to String in Python - GeeksforGeeks
%s placeholder inserts values into a string and automatically converts them to string format. ... Explanation: %s acts as a placeholder for the value and Python converts the integer into a string before inserting it.
Published   2 weeks ago
Discussions

Cannot use str() to convert an int to a string.
Is maxhealth and maxhunger also integers? print(f"\nHealth: {healthstat}/{maxhealth}") More on reddit.com
🌐 r/learnpython
13
23
January 18, 2022
python - Short way to convert string to int - Stack Overflow
I usually do this to convert string to int: my_input = int(my_input) but I was wondering if there was a less clumsy way, because it feels kind of long. More on stackoverflow.com
🌐 stackoverflow.com
i am a beginner at python and im trying to convert string to int but it doesn't work
What? You can't convert "sadsad" to an int. What would that even mean? Don't you mean you want to convert the integer 170 to a string, instead? More on reddit.com
🌐 r/learnpython
21
1
August 8, 2023
Converting a string input to a number or integer
This would require an actual number be entered. You could drop the input part and just use your variable in place of number_question to verify the string is a number. while True: number_question = input("Please enter a number ") try: val = int(number_question) break except ValueError: try: val = float(number_question) break except ValueError: print("I'm sorry,",f'"{number_question.upper()}" is a string, not a number.') print (f'You entered the number {val}.') More on reddit.com
🌐 r/learnpython
7
1
October 2, 2024
🌐
Replit
replit.com › home › discover › how to convert an int to a string in python
How to convert an int to a string in Python | Replit
The fix is to explicitly convert the integer to a string using the str() function. By wrapping age in str(age), you tell Python to treat the number as text. This allows the + operator to correctly concatenate all parts into a single string.
🌐
Real Python
realpython.com › convert-python-string-to-int
How to Convert a Python String to int – Real Python
January 16, 2021 - There are several ways to represent integers in Python. In this quick and practical tutorial, you'll learn how you can store integers using int and str as well as how you can convert a Python string to an int and vice versa.
🌐
Scaler
scaler.com › home › topics › how to convert int to string in python
How to Convert int to string in Python - Scaler Topics
May 12, 2024 - There are several methods to convert an integer into a string, like, str() function, using %s keyword, the .format() function, and f-strings. There are four ways for converting an integer into a string in Python.
🌐
FavTutor
favtutor.com › blogs › int-to-string-python
4 Ways to Convert Int to String in Python | FavTutor
September 14, 2021 - You’ve learned so much about the integer, strings, and different methods used to convert an integer (Int) to string in python. These methods include the use of the str() method, “%s” operator, format() method, and F-strings to convert ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-convert-integers-to-strings-in-python-3
How To Convert Integers to Strings in Python 3 | DigitalOcean
September 3, 2020 - We can convert numbers to strings using the str() method. We’ll pass either a number or a variable into the parentheses of the method and then that numeric value will be converted into a string value.
Find elsewhere
🌐
SheCodes
shecodes.io › athena › 2142-converting-an-integer-to-string-in-python
[Python] - Converting an Integer to String in Python - | SheCodes
Learn how to convert an integer to a string in Python by using the `str()` function or casting the integer as a string.
🌐
LabEx
labex.io › tutorials › python-how-to-convert-an-integer-to-a-string-in-python-397677
How to convert an integer to a string in Python | LabEx
You can also convert an integer to a string by using the + operator to concatenate the integer with an empty string. ## Example integer_value = 42 string_value = integer_value + '' print(string_value) ## Output: '42' Python 3.6 introduced a new way to convert integers to strings using formatted string literals, also known as f-strings.
🌐
W3Schools
w3schools.in › python › examples › convert-int-to-string
Python Program to Convert Int to String - W3schools
Using f-strings feature (available in Python 3.6 and above). Using % operator (also known as the "string formatting operator"). To convert an integer to a string in Python, the str() function can be used. This function takes an integer as an argument and returns the corresponding string ...
🌐
Reddit
reddit.com › r/learnpython › cannot use str() to convert an int to a string.
r/learnpython on Reddit: Cannot use str() to convert an int to a string.
January 18, 2022 -

Okay, so I had a crash course in Python a looooong time ago, and that's all the prior experience I have with it. I'm starting to dabble in it again, playing around with it to make a text-based game.

In this game, you have stats - Speed, Health, etc etc. Each of these is a number (an int).

I am trying to define a function names statdisplay() so that when I call on it, it prints out your stats. So, if you have a Speed of 1, and the max Speed you can increase that stat to is 5, one of the printed lines would be:

Speed: 1 / 5

This was my ORIGINAL code:

 print("\nHealth: " + healthstat + " / " + maxhealth)
 print("\nHunger: " + hungerstat + " / " + maxhunger)

 print("\nStalking Skill: " + stalkingstat + " / 5")
 print("\nHunting Skill: " + huntingstat + " / 5")

 print("\nSpeed: " + speedstat + " / 5")
 print("\nStrength: " + speedstat + " / 5")

But then I got the following error:

TypeError: can only concatenate str (not "int") to str

So I mentally facepalmed myself and looked up how to convert an int to a string, and I keep reading that you can use the str() function (I know there are other ways, but I'm taking baby steps here trying to jog my memory on how everything works before I go doing everything by what's deemed "most appropriate").

This is my NEW code with that in mind:

 print("\nHealth: " + str(healthstat) + " / " + maxhealth)
 print("\nHunger: " + str(hungerstat) + " / " + maxhunger)

 print("\nStalking Skill: " + str(stalkingstat) + " / 5")
 print("\nHunting Skill: " + str(huntingstat) + " / 5")

 print("\nSpeed: " + str(speedstat) + " / 5")
 print("\nStrength: " + str(speedstat) + " / 5")

...and yet I am still getting the following error:

TypeError: can only concatenate str (not "int") to str

I can't seem to figure out what's wrong. I do not have the str() function defined as anything else. str() also doesn't seem to come from any special libraries that I'd need to import, but maybe I'm wrong there (I tried to look it up)... the only import I currently have is "import random".

My only other thought is that maybe it's a Google Colab thing, as that's where I'm currently running my code since this wasn't anything serious and I tend to go between two different computers.

Any help would be much appreciated!

Edit: accidentally had the new code in both code boxes.

🌐
Unstop
unstop.com › home › blog › convert int to string in python (6 methods with examples)
Convert Int To String In Python (6 Methods With Examples)
April 11, 2024 - One common approach to convert int to string in Python is to use built-in functions like str(), which directly converts an integer to its string representation. For instance, str(123) returns the string '123'.
🌐
Mimo
mimo.org › tutorials › python › how-to-convert-integer-to-string-in-python
How to Convert Integer to String in Python
Learn how to convert an int to a string in Python with str() and f-strings, avoid TypeError, and build clean filenames and messages.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-to-int-int-to-string
Python String to Int, Int to String | DigitalOcean
August 4, 2022 - See the following example to understand the conversion of string to int with the base argument. num = '123' # print the original string print('The original string :', num) # considering '123' be in base 10, convert it to base 10 print('Base 10 to base 10:', int(num)) # considering '123' be in base 8, convert it to base 10 print('Base 8 to base 10 :', int(num, base=8)) # considering '123' be in base 6, convert it to base 10 print('Base 6 to base 10 :', int(num, base=6))
🌐
Career Karma
careerkarma.com › blog › python › python string to int() and int to string tutorial: type conversion in python
Python String to Int() and Int to String Tutorial: Type Conversion in Python
December 1, 2023 - We’ll also discuss how to use str() to convert an integer to a string. When you’re programming in Python, the data you are working with will be stored in a number of different ways. If you’re working with text, your data will be stored as a string.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-int-to-string-in-python
How to convert int to string in Python?
March 25, 2026 - The str() function is the most straightforward method for converting integers to strings. Use f-strings for modern Python applications and when embedding integers within larger strings.
🌐
Runestone Academy
runestone.academy › ns › books › published › pythonds › Recursion › pythondsConvertinganIntegertoaStringinAnyBase.html
5.5. Converting an Integer to a String in Any Base — Problem Solving with Algorithms and Data Structures
Using integer division to divide 769 by 10, we get 76 with a remainder of 9. This gives us two good results. First, the remainder is a number less than our base that can be converted to a string immediately by lookup. Second, we get a number that is smaller than our original and moves us toward the base case of having a single number less than our base.
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-string-to-integer-in-python
Convert String to Int in Python - GeeksforGeeks
If the string contains non-numeric characters or is empty, int() will raise a ValueError. int() function automatically handles leading and trailing whitespaces, so int() function trims whitespaces and converts the core numeric part to an integer.
Published   3 weeks ago