HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonCritical

Convert integer to string in Python

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
stringconvertintegerpython

Problem

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:

  • 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.

Code Snippets

>>> str(42)
'42'

>>> int('42')
42

Context

Stack Overflow Q#961632, score: 2357

Revisions (0)

No revisions yet.