operation with strings and numbers

Andrew Dalke adalke at mindspring.com
Fri Oct 15 13:34:35 EDT 2004


Hellas wrote:
> the resulted is "_11,278" where "_" is a space
> I must to change the formatting of this number in "011278" but I don't known 
> what to do

So you want to replace the any space with a '0' and get rid
of the ',' (in this case the decimal sign because you're
using an Italian locale, as compared to my US use of '.')

Try this

 >>> s = " 11,278"
 >>> s.replace(" ", "0")
'011,278'
 >>> s.replace(" ", "0").replace(",", "")
'011278'
 >>>

Another solution is to use the 'zfill' method of strings.

 >>> s = "%6.3f" % p
 >>> s
'11.378'
 >>> s.replace(".", "").zfill(6)
'011378'
 >>>

				Andrew
				dalke at dalkescientific.com



More information about the Python-list mailing list