>>> 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
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Convert a String to a Number (int, float) in Python | note.nkmk.me
April 29, 2025 - Use str() to convert a number to a string. Built-in Functions - str() โ€” Python 3.13.3 documentation
๐ŸŒ
Python Principles
pythonprinciples.com โ€บ blog โ€บ converting-integer-to-string-in-python
Converting integer to string in Python โ€“ Python Principles
To convert an integer to a string, use the str() built-in function. The function takes an int as input and produces a string as its output. Here are some examples.
Discussions

Convert integer to string in Python - Stack Overflow
How do I convert an integer to a string? 42 โŸถ "42" For the reverse, see How do I parse a string to a float or int?. Floats can be handled similarly, but handling the decimal points ca... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
python - How do I parse a string to a float or int? - Stack Overflow
How can I convert an str to a float? "545.2222" -> 545.2222 Or an str to a int? "31" -> 31 For the reverse, see Convert integer to string in Python and Converting a float... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python: Replace Numeric Value With String - Stack Overflow
I'm trying to replace a value entered by a user with a string to make the output cleaner I thought an if statement would help, but I'm not sure how it would tie in with my intended output def ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 25, 2018
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ convert-strings-to-numbers-and-numbers-to-strings-in-python
Convert Strings to Numbers and Numbers to Strings in Python
January 29, 2020 - The str() function can be used to change any numeric type to a string. The function str() is available from Python 3.0+ since the strings in Python 3.0+ are Unicode by default.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-convert-data-types-in-python-3
How To Convert Data Types in Python 3 | DigitalOcean
August 20, 2021 - Strings are a common form of data in computer programs, and we may need to convert strings to numbers or numbers to strings fairly often, especially when we are taking in user-generated data. We can convert numbers to strings through using the str() method. Weโ€™ll pass either a number or a ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ convert-integer-to-string-in-python
Convert integer to string in Python - GeeksforGeeks
Explanation: str(n) converts n to a string, resulting in '42'. For Python 3.6 or later, f-strings provide a quick way to format and convert values.
Published ย  April 25, 2025
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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 - The syntax for using the str() function in python is to write the value to be typecasted inside the parenthesis. ... The syntax for using the %s keyword is enclosing %s in quotation marks and then writing % integer after it. ... The syntax for using the .format() function in int to string ...
Top answer
1 of 16
3126
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
2 of 16
606

Python2 method to check if a string is a float:

def is_float(value):
  if value is None:
      return False
  try:
      float(value)
      return True
  except:
      return False

For the Python3 version of is_float see: Checking if a string can be converted to float in Python

A longer and more accurate name for this function could be: is_convertible_to_float(value)

What is, and is not a float in Python may surprise you:

The below unit tests were done using python2. Check it that Python3 has different behavior for what strings are convertable to float. One confounding difference is that any number of interior underscores are now allowed: (float("1_3.4") == float(13.4)) is True

val                   is_float(val) Note
--------------------  ----------   --------------------------------
""                    False        Blank string
"127"                 True         Passed string
True                  True         Pure sweet Truth
"True"                False        Vile contemptible lie
False                 True         So false it becomes true
"123.456"             True         Decimal
"      -127    "      True         Spaces trimmed
"\t\n12\r\n"          True         whitespace ignored
"NaN"                 True         Not a number
"NaNanananaBATMAN"    False        I am Batman
"-iNF"                True         Negative infinity
"123.E4"              True         Exponential notation
".1"                  True         mantissa only
"1_2_3.4"             False        Underscores not allowed
"12 34"               False        Spaces not allowed on interior
"1,234"               False        Commas gtfo
u'\x30'               True         Unicode is fine.
"NULL"                False        Null is not special
0x3fade               True         Hexadecimal
"6e7777777777777"     True         Shrunk to infinity
"1.797693e+308"       True         This is max value
"infinity"            True         Same as inf
"infinityandBEYOND"   False        Extra characters wreck it
"12.34.56"            False        Only one dot allowed
u'ๅ››'                 False        Japanese '4' is not a float.
"#56"                 False        Pound sign
"56%"                 False        Percent of what?
"0E0"                 True         Exponential, move dot 0 places
0**0                  True         0___0  Exponentiation
"-5e-5"               True         Raise to a negative number
"+1e1"                True         Plus is OK with exponent
"+1e1^5"              False        Fancy exponent not interpreted
"+1e1.3"              False        No decimals in exponent
"-+1"                 False        Make up your mind
"(1)"                 False        Parenthesis is bad

You think you know what numbers are? You are not so good as you think! Not big surprise.

Don't use this code on life-critical software!

Catching broad exceptions this way, killing canaries and gobbling the exception creates a tiny chance that a valid float as string will return false. The float(...) line of code can failed for any of a thousand reasons that have nothing to do with the contents of the string. But if you're writing life-critical software in a duck-typing prototype language like Python, then you've got much larger problems.

๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-convert-int-to-string-in-python
How to convert int to string in Python - Javatpoint
How to convert int to string in Python with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, basics, data types, operators, etc.
๐ŸŒ
MakeUseOf
makeuseof.com โ€บ home โ€บ programming โ€บ 4 ways to convert an integer to a string in python
4 Ways to Convert an Integer to a String in Python
March 9, 2022 - To use positional formatting, use the "%s" % prefix in front of the number, and Python will do the rest. ... # The integer value Number = 100 # Convert the integer into a string using positional formatting Str_value = "%s" % Number # Print the ...
๐ŸŒ
DataScience Made Simple
datasciencemadesimple.com โ€บ home โ€บ convert numeric column to character in pandas python (integer to string)
Convert numeric column to character in pandas python (integer to string) - DataScience Made Simple
January 29, 2023 - Converting numeric column to character in pandas python is accomplished using astype() function. astype() function converts or Typecasts integer column to string column in pandas.
Top answer
1 of 2
1

Try

 def main() :
    number = int(input("Enter your number: "))
    base = int(input("Convert to\n" \
    "   Binary[2] - Octal[8] - Hexadecimal[16]: "))
    base_string = "None"        

    if base == 2 :
        base_string = "binary"
     elif base == 8 :
        base_string = "octal"
    else:
        base_string = "hexadecimal"

    print("\n {} in {} is: {}".format(str(number), base_string, str(convert(number, 10, base))))


  def convert(fromNum, fromBase, toBase) :
    toNum = 0
    power = 0

    while fromNum > 0 :
        toNum += fromBase ** power * (fromNum % toBase)
        fromNum //= toBase
        power += 1
    return toNum

main()

Your issue was the "binary" part in the if-statement. It has virtually no effect neither on your code nor on your output. You have to store the literal representation ("binary",...) in some variable ("base_string"). Then you can use this variable in your output.

2 of 2
1

As an aside, it looks like your base conversion won't actually do what you want. You should look at How to convert an integer in any base to a string? to do the conversion properly (hexadecimal has letters A-F in it, those aren't handled by your code, for example).

To accept a name instead of a number, you need to change this line of code:

base = int(input("Convert to\n   Binary[2] - Octal[8] - Hexadecimal[16]: "))

What's happening here? input() takes a line from stdin. In the interactive case, this means the user types something (hopefully a number) and then hits enter. We get that string. Then int converts that string to a number.

Your convert expects base to be a number. You want inputs like "binary" to correspond to base = 2. One way to achieve this is with a dict. It can map strings to numbers:

base_name_to_base = {'binary': 2, 'octal': 8, 'hexadecimal': 16}
base = base_name_to_base[input('Choose a base (binary, octal, hexadecimal): ')]

Note that base_name_to_base[x] can fail (raise KeyError) if x is not a key in the dict. So you want to handle this (what if the user enters "blah"?):

while True:
    try:
        base = base_name_to_base[input('Choose a base (binary, octal, hexadecimal): ')]
        break
    except KeyError:
        pass

This will loop until we hit the break (which only happens when indexing into base_name_to_base doesn't raise the key error.

Additionally, you may want to handle different cases (ex. "BINARY") or arbitrary whitespace (ex. " binary "). You can do this by calling .lower() and .strip() on the string returned by input().

๐ŸŒ
PyTutorial
pytutorial.com โ€บ python-convert-number-to-string-str-method-guide
PyTutorial | Python Convert Number to String | str() Method Guide
February 8, 2026 - Learn how to convert numbers to strings in Python using str(), f-strings, and format() with clear examples for integers, floats, and data handling.
๐ŸŒ
Medium
mechatronicslab-net.medium.com โ€บ how-to-convert-an-integer-to-a-string-in-python-9b0724f73cd8
How to Convert an Integer to a String in Python | by MechatronicsLAB | Medium
March 20, 2025 - This guide explores different methods to convert an integer to a string in Python. ... Displaying numbers in a user-friendly format. Concatenating numbers with text in messages. Storing numerical values in databases as text.
๐ŸŒ
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 - The most common way to convert int to string in Python is the inbuilt str() function, which can convert any data type into a string. Other approaches include the use of formatted strings (f-strings), the format() method, chr() method, and more.
๐ŸŒ
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.