Search code examples
pythonstringinteger

Convert integer to string in Python


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 can be tricky because floating-point values are not precise. See Converting a float to a string without rounding it for more specific advice.


Solution

  • >>> str(42)
    '42'
    
    >>> int('42')
    42
    

    Links to the documentation:

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