The Python Oracle

Convert integer to string in Python

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3

--

Chapters
00:00 Convert Integer To String In Python
00:27 Answer 1 Score 156
00:35 Accepted Answer Score 2350
00:59 Answer 3 Score 19
01:08 Answer 4 Score 72
01:33 Thank you

--

Full question
https://stackoverflow.com/questions/9616...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #string #integer

#avk47



ACCEPTED ANSWER

Score 2350


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




ANSWER 2

Score 156


Try this:

str(i)



ANSWER 3

Score 72


There is no typecast and no type coercion in Python. You have to convert your variable in an explicit way.

To convert an object into a string you use the str() function. It works with any object that has a method called __str__() defined. In fact

str(a)

is equivalent to

a.__str__()

The same if you want to convert something to int, float, etc.




ANSWER 4

Score 19


>>> i = 5
>>> print "Hello, world the number is " + i
TypeError: must be str, not int
>>> s = str(i)
>>> print "Hello, world the number is " + s
Hello, world the number is 5