[Tutor] Remove specific chars from a string

Kent Johnson kent37 at tds.net
Mon Apr 14 22:39:42 CEST 2008


Dick Moores wrote:
> def sigDigits(n):
>     """
>     Strips any real decimal (as string) to just its significant digits,
>     then returns its length, the number of significant digits.
>     Examples: "-345" -> "345" -> 3;
>     "3.000" -> "3000" -> 4
>     "0.0001234" -> "1234" -> 4;
>     "10.0001234" -> "100001234" -> 9
>     "7.2345e+543" -> "72345" -> 5
>     """
>     s = str(n).lstrip("-+0")
>     s = s.lstrip(".")
>     s = s.lstrip("0")
>     s = s.lstrip(".")

Why the repeated strips? Couldn't this all just be done with 
lstrip('-+0.') ?

How many significant digits are in 123000?

Kent


More information about the Tutor mailing list