Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting integer to string in Python

I want to convert an integer to a string in Python. I am typecasting it in vain:

d = 15 d.str() 

When I try to convert it to string, it's showing an error like int doesn't have any attribute called str.

like image 732
Hick Avatar asked Jun 07 '09 10:06

Hick


People also ask

How do you convert int to string in Python?

In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string. But use of the str() is not the only way to do so. This type of conversion can also be done using the “%s” keyword, the .

How do you convert integers to strings?

The easiest way to convert int to String is very simple. Just add to int or Integer an empty string "" and you'll get your int as a String. It happens because adding int and String gives you a new String. That means if you have int x = 5 , just define x + "" and you'll get your new String.

How do you convert int to string without using library functions in Python?

Convert int to string python without str() function Simple example code keeps dividing a given int value by 10 and prepending the remainder to the output string. Use the ordinal number of '0' plus the remainder to obtain the ordinal number of the remainder, and then convert it to string using the chr function.

How do you convert a variable to a string in Python?

Converting Numbers to Strings We can convert numbers to strings through 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.


2 Answers

>>> str(10) '10' >>> int('10') 10 

Links to the documentation:

  • int()
  • str()

Conversion to a string is done with the builtin str() function, which basically calls the __str__() method of its parameter.

like image 187
Bastien Léonard Avatar answered Oct 11 '22 18:10

Bastien Léonard


Try this:

str(i) 
like image 41
Lasse V. Karlsen Avatar answered Oct 11 '22 17:10

Lasse V. Karlsen