Pages

mercredi 21 avril 2010

convert between numbers and str in python

In python, if we want to convert a float into a string, simply use str(10.2)

if want to represent the data with the desired number of digits, try:

"%.3f" %1.32299, for example.
This will convert the float number 1.32299 to a string '1.323'.

To get a string with digits less than 3, there could be 3 approach:
"%.2f" %1.32299 : convert the float to a string '1.32'
or repr(round(1.239,2))
or str(round(1.239,2))

if to convert int to str, use
repr(10) or str(10)

If to convert a list of int into a list of str, use map:
a=range(10)
map(str,a)

Aucun commentaire:

Enregistrer un commentaire