[Tutor] Count for loops

Marc Tompkins marc.tompkins at gmail.com
Tue Apr 11 15:18:42 EDT 2017


On Tue, Apr 11, 2017 at 9:48 AM, Rafael Knuth <rafael.knuth at gmail.com>
wrote:

> I tested this approach, and I noticed one weird thing:
>
> Pi_Number = str(3.14159265358979323846264338327950288419716939)
> Pi_Number = "3" + Pi_Number[2:]
> print(Pi_Number)
>
> == RESTART: C:\Users\Rafael\Documents\01 - BIZ\CODING\Python
> Code\PPC_56.py ==
> 3141592653589793
> >>>
>
> How come that not the entire string is being printed, but only the
> first 16 digits?
>

That's due to how str() works; it's simply making (its own conception) of a
pretty representation of what's fed to it, which in the case of
floating-point numbers means that they're represented to 16 digits
precision.
str(3.14159265358979323846264338327950288419716939) is _not_ the same as
    "3.14159265358979323846264338327950288419716939"

>From the docs: "Return a string containing a nicely printable
representation of an object. For strings, this returns the string itself.
The difference with repr(object) is that str(object) does not always
attempt to return a string that is acceptable to eval()
<https://docs.python.org/2/library/functions.html#eval>; its goal is to
return a printable string."


More information about the Tutor mailing list