diff options
author | Fred Drake <fdrake@acm.org> | 2001-12-04 19:20:43 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2001-12-04 19:20:43 (GMT) |
commit | 6016dbeccaadb94eb7535230f29fa4b4e2579897 (patch) | |
tree | 189884b0f48c084e0adaab93e4af2b198f9a98eb /Doc/tut | |
parent | fa78d0fbe45a7f16d5d30c3d4e3ad30dc4933e8f (diff) | |
download | cpython-6016dbeccaadb94eb7535230f29fa4b4e2579897.zip cpython-6016dbeccaadb94eb7535230f29fa4b4e2579897.tar.gz cpython-6016dbeccaadb94eb7535230f29fa4b4e2579897.tar.bz2 |
Talk about str() in the discussion of string representations of values, and
give examples for which str() and repr() yield different results.
This closes SF bug #485446.
Diffstat (limited to 'Doc/tut')
-rw-r--r-- | Doc/tut/tut.tex | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/Doc/tut/tut.tex b/Doc/tut/tut.tex index e90c267..f02b3ff 100644 --- a/Doc/tut/tut.tex +++ b/Doc/tut/tut.tex @@ -2638,11 +2638,34 @@ string to be applied to the right argument, and returns the string resulting from this formatting operation. One question remains, of course: how do you convert values to strings? -Luckily, Python has a way to convert any value to a string: pass it to -the \function{repr()} function, or just write the value between -reverse quotes (\code{``}). Some examples: +Luckily, Python has ways to convert any value to a string: pass it to +the \function{repr()} or \function{str()} functions, or just write +the value between reverse quotes (\code{``}, equivalent to +\function{repr()}). + +The \function{str()} function is meant to return representations of +values which are fairly human-readable, while \function{repr()} is +meant to generate representations which can be read by the interpreter +(or will force a \exception{SyntaxError} if there is not equivalent +syntax). For objects which don't have a particular representation for +human consumption, \function{str()} will return the same value as +\function{repr()}. Many values, such as numbers or structures like +lists and dictionaries, have the same representation using either +function. Strings and floating point numbers, in particular, have two +distinct representations. + +Some examples: \begin{verbatim} +>>> s = 'Hello, world.' +>>> str(s) +'Hello, world.' +>>> `s` +"'Hello, world.'" +>>> str(0.1) +'0.1' +>>> `0.1` +'0.10000000000000001' >>> x = 10 * 3.25 >>> y = 200 * 200 >>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...' |