snippetpythonCriticalCanonical
Convert integer to string in Python
Viewed 0 times
stringconvertpythoninteger
Problem
How do I convert an integer to a string?
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.
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')
42Links 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')
42Context
Stack Overflow Q#961632, score: 2357
Revisions (0)
No revisions yet.