blob: 6d8f89008b8a116f78569c31e252b8f9f7381d9c [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. _tut-informal:
2
3**********************************
4An Informal Introduction to Python
5**********************************
6
7In the following examples, input and output are distinguished by the presence or
8absence of prompts (``>>>`` and ``...``): to repeat the example, you must type
9everything after the prompt, when the prompt appears; lines that do not begin
10with a prompt are output from the interpreter. Note that a secondary prompt on a
11line by itself in an example means you must type a blank line; this is used to
12end a multi-line command.
13
Georg Brandl8ec7f652007-08-15 14:28:01 +000014Many of the examples in this manual, even those entered at the interactive
15prompt, include comments. Comments in Python start with the hash character,
Georg Brandl3ce0dee2008-09-13 17:18:11 +000016``#``, and extend to the end of the physical line. A comment may appear at the
17start of a line or following whitespace or code, but not within a string
Georg Brandlb19be572007-12-29 10:57:00 +000018literal. A hash character within a string literal is just a hash character.
Georg Brandl3ce0dee2008-09-13 17:18:11 +000019Since comments are to clarify code and are not interpreted by Python, they may
20be omitted when typing in examples.
Georg Brandl8ec7f652007-08-15 14:28:01 +000021
22Some examples::
23
24 # this is the first comment
25 SPAM = 1 # and this is the second comment
26 # ... and now a third!
27 STRING = "# This is not a comment."
28
29
30.. _tut-calculator:
31
32Using Python as a Calculator
33============================
34
35Let's try some simple Python commands. Start the interpreter and wait for the
36primary prompt, ``>>>``. (It shouldn't take long.)
37
38
39.. _tut-numbers:
40
41Numbers
42-------
43
44The interpreter acts as a simple calculator: you can type an expression at it
45and it will write the value. Expression syntax is straightforward: the
46operators ``+``, ``-``, ``*`` and ``/`` work just like in most other languages
47(for example, Pascal or C); parentheses can be used for grouping. For example::
48
49 >>> 2+2
50 4
51 >>> # This is a comment
52 ... 2+2
53 4
54 >>> 2+2 # and a comment on the same line as code
55 4
56 >>> (50-5*6)/4
57 5
58 >>> # Integer division returns the floor:
59 ... 7/3
60 2
61 >>> 7/-3
62 -3
63
64The equal sign (``'='``) is used to assign a value to a variable. Afterwards, no
65result is displayed before the next interactive prompt::
66
67 >>> width = 20
68 >>> height = 5*9
69 >>> width * height
70 900
71
72A value can be assigned to several variables simultaneously::
73
74 >>> x = y = z = 0 # Zero x, y and z
75 >>> x
76 0
77 >>> y
78 0
79 >>> z
80 0
81
Georg Brandl3ce0dee2008-09-13 17:18:11 +000082Variables must be "defined" (assigned a value) before they can be used, or an
83error will occur::
84
85 >>> # try to access an undefined variable
86 ... n
Georg Brandlc62ef8b2009-01-03 20:55:06 +000087 Traceback (most recent call last):
Georg Brandl3ce0dee2008-09-13 17:18:11 +000088 File "<stdin>", line 1, in <module>
89 NameError: name 'n' is not defined
90
Georg Brandl8ec7f652007-08-15 14:28:01 +000091There is full support for floating point; operators with mixed type operands
92convert the integer operand to floating point::
93
94 >>> 3 * 3.75 / 1.5
95 7.5
96 >>> 7.0 / 2
97 3.5
98
99Complex numbers are also supported; imaginary numbers are written with a suffix
100of ``j`` or ``J``. Complex numbers with a nonzero real component are written as
101``(real+imagj)``, or can be created with the ``complex(real, imag)`` function.
102::
103
104 >>> 1j * 1J
105 (-1+0j)
106 >>> 1j * complex(0,1)
107 (-1+0j)
108 >>> 3+1j*3
109 (3+3j)
110 >>> (3+1j)*3
111 (9+3j)
112 >>> (1+2j)/(1+1j)
113 (1.5+0.5j)
114
115Complex numbers are always represented as two floating point numbers, the real
116and imaginary part. To extract these parts from a complex number *z*, use
117``z.real`` and ``z.imag``. ::
118
119 >>> a=1.5+0.5j
120 >>> a.real
121 1.5
122 >>> a.imag
123 0.5
124
125The conversion functions to floating point and integer (:func:`float`,
126:func:`int` and :func:`long`) don't work for complex numbers --- there is no one
127correct way to convert a complex number to a real number. Use ``abs(z)`` to get
128its magnitude (as a float) or ``z.real`` to get its real part. ::
129
130 >>> a=3.0+4.0j
131 >>> float(a)
132 Traceback (most recent call last):
133 File "<stdin>", line 1, in ?
134 TypeError: can't convert complex to float; use abs(z)
135 >>> a.real
136 3.0
137 >>> a.imag
138 4.0
139 >>> abs(a) # sqrt(a.real**2 + a.imag**2)
140 5.0
Georg Brandl8ec7f652007-08-15 14:28:01 +0000141
142In interactive mode, the last printed expression is assigned to the variable
143``_``. This means that when you are using Python as a desk calculator, it is
144somewhat easier to continue calculations, for example::
145
146 >>> tax = 12.5 / 100
147 >>> price = 100.50
148 >>> price * tax
149 12.5625
150 >>> price + _
151 113.0625
152 >>> round(_, 2)
153 113.06
Georg Brandl8ec7f652007-08-15 14:28:01 +0000154
155This variable should be treated as read-only by the user. Don't explicitly
156assign a value to it --- you would create an independent local variable with the
157same name masking the built-in variable with its magic behavior.
158
159
160.. _tut-strings:
161
162Strings
163-------
164
165Besides numbers, Python can also manipulate strings, which can be expressed in
166several ways. They can be enclosed in single quotes or double quotes::
167
168 >>> 'spam eggs'
169 'spam eggs'
170 >>> 'doesn\'t'
171 "doesn't"
172 >>> "doesn't"
173 "doesn't"
174 >>> '"Yes," he said.'
175 '"Yes," he said.'
176 >>> "\"Yes,\" he said."
177 '"Yes," he said.'
178 >>> '"Isn\'t," she said.'
179 '"Isn\'t," she said.'
180
Senthil Kumaranbf024292010-11-08 02:12:57 +0000181The interpreter prints the result of string operations in the same way as they
182are typed for input: inside quotes, and with quotes and other funny characters
183escaped by backslashes, to show the precise value. The string is enclosed in
184double quotes if the string contains a single quote and no double quotes, else
185it's enclosed in single quotes. The :keyword:`print` statement produces a more
186readable output for such input strings.
187
Georg Brandl8ec7f652007-08-15 14:28:01 +0000188String literals can span multiple lines in several ways. Continuation lines can
189be used, with a backslash as the last character on the line indicating that the
190next line is a logical continuation of the line::
191
192 hello = "This is a rather long string containing\n\
193 several lines of text just as you would do in C.\n\
194 Note that whitespace at the beginning of the line is\
195 significant."
196
197 print hello
198
Georg Brandl565569b2010-06-27 10:47:47 +0000199Note that newlines still need to be embedded in the string using ``\n`` -- the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000200newline following the trailing backslash is discarded. This example would print
Georg Brandlbf58d802009-09-03 07:27:26 +0000201the following:
202
203.. code-block:: text
Georg Brandl8ec7f652007-08-15 14:28:01 +0000204
205 This is a rather long string containing
206 several lines of text just as you would do in C.
207 Note that whitespace at the beginning of the line is significant.
208
Georg Brandl8ec7f652007-08-15 14:28:01 +0000209Or, strings can be surrounded in a pair of matching triple-quotes: ``"""`` or
210``'''``. End of lines do not need to be escaped when using triple-quotes, but
211they will be included in the string. ::
212
213 print """
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000214 Usage: thingy [OPTIONS]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000215 -h Display this usage message
216 -H hostname Hostname to connect to
217 """
218
Georg Brandlbf58d802009-09-03 07:27:26 +0000219produces the following output:
220
221.. code-block:: text
Georg Brandl8ec7f652007-08-15 14:28:01 +0000222
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000223 Usage: thingy [OPTIONS]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000224 -h Display this usage message
225 -H hostname Hostname to connect to
226
Georg Brandl186188d2009-03-31 20:56:32 +0000227If we make the string literal a "raw" string, ``\n`` sequences are not converted
228to newlines, but the backslash at the end of the line, and the newline character
229in the source, are both included in the string as data. Thus, the example::
230
231 hello = r"This is a rather long string containing\n\
232 several lines of text much as you would do in C."
233
234 print hello
235
Georg Brandlbf58d802009-09-03 07:27:26 +0000236would print:
237
238.. code-block:: text
Georg Brandl186188d2009-03-31 20:56:32 +0000239
240 This is a rather long string containing\n\
241 several lines of text much as you would do in C.
242
Georg Brandl8ec7f652007-08-15 14:28:01 +0000243The interpreter prints the result of string operations in the same way as they
244are typed for input: inside quotes, and with quotes and other funny characters
245escaped by backslashes, to show the precise value. The string is enclosed in
246double quotes if the string contains a single quote and no double quotes, else
247it's enclosed in single quotes. (The :keyword:`print` statement, described
248later, can be used to write strings without quotes or escapes.)
249
250Strings can be concatenated (glued together) with the ``+`` operator, and
251repeated with ``*``::
252
253 >>> word = 'Help' + 'A'
254 >>> word
255 'HelpA'
256 >>> '<' + word*5 + '>'
257 '<HelpAHelpAHelpAHelpAHelpA>'
258
259Two string literals next to each other are automatically concatenated; the first
260line above could also have been written ``word = 'Help' 'A'``; this only works
261with two literals, not with arbitrary string expressions::
262
263 >>> 'str' 'ing' # <- This is ok
264 'string'
265 >>> 'str'.strip() + 'ing' # <- This is ok
266 'string'
267 >>> 'str'.strip() 'ing' # <- This is invalid
268 File "<stdin>", line 1, in ?
269 'str'.strip() 'ing'
270 ^
271 SyntaxError: invalid syntax
272
273Strings can be subscripted (indexed); like in C, the first character of a string
274has subscript (index) 0. There is no separate character type; a character is
275simply a string of size one. Like in Icon, substrings can be specified with the
276*slice notation*: two indices separated by a colon. ::
277
278 >>> word[4]
279 'A'
280 >>> word[0:2]
281 'He'
282 >>> word[2:4]
283 'lp'
284
285Slice indices have useful defaults; an omitted first index defaults to zero, an
286omitted second index defaults to the size of the string being sliced. ::
287
288 >>> word[:2] # The first two characters
289 'He'
290 >>> word[2:] # Everything except the first two characters
291 'lpA'
292
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000293Unlike a C string, Python strings cannot be changed. Assigning to an indexed
Georg Brandl8ec7f652007-08-15 14:28:01 +0000294position in the string results in an error::
295
296 >>> word[0] = 'x'
297 Traceback (most recent call last):
298 File "<stdin>", line 1, in ?
Georg Brandl5e88eea2009-05-17 08:10:27 +0000299 TypeError: object does not support item assignment
Georg Brandl8ec7f652007-08-15 14:28:01 +0000300 >>> word[:1] = 'Splat'
301 Traceback (most recent call last):
302 File "<stdin>", line 1, in ?
Georg Brandl5e88eea2009-05-17 08:10:27 +0000303 TypeError: object does not support slice assignment
Georg Brandl8ec7f652007-08-15 14:28:01 +0000304
305However, creating a new string with the combined content is easy and efficient::
306
307 >>> 'x' + word[1:]
308 'xelpA'
309 >>> 'Splat' + word[4]
310 'SplatA'
311
312Here's a useful invariant of slice operations: ``s[:i] + s[i:]`` equals ``s``.
313::
314
315 >>> word[:2] + word[2:]
316 'HelpA'
317 >>> word[:3] + word[3:]
318 'HelpA'
319
320Degenerate slice indices are handled gracefully: an index that is too large is
321replaced by the string size, an upper bound smaller than the lower bound returns
322an empty string. ::
323
324 >>> word[1:100]
325 'elpA'
326 >>> word[10:]
327 ''
328 >>> word[2:1]
329 ''
330
331Indices may be negative numbers, to start counting from the right. For example::
332
333 >>> word[-1] # The last character
334 'A'
335 >>> word[-2] # The last-but-one character
336 'p'
337 >>> word[-2:] # The last two characters
338 'pA'
339 >>> word[:-2] # Everything except the last two characters
340 'Hel'
341
342But note that -0 is really the same as 0, so it does not count from the right!
343::
344
345 >>> word[-0] # (since -0 equals 0)
346 'H'
347
348Out-of-range negative slice indices are truncated, but don't try this for
349single-element (non-slice) indices::
350
351 >>> word[-100:]
352 'HelpA'
353 >>> word[-10] # error
354 Traceback (most recent call last):
355 File "<stdin>", line 1, in ?
356 IndexError: string index out of range
357
358One way to remember how slices work is to think of the indices as pointing
359*between* characters, with the left edge of the first character numbered 0.
360Then the right edge of the last character of a string of *n* characters has
361index *n*, for example::
362
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000363 +---+---+---+---+---+
Georg Brandl8ec7f652007-08-15 14:28:01 +0000364 | H | e | l | p | A |
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000365 +---+---+---+---+---+
366 0 1 2 3 4 5
Georg Brandl8ec7f652007-08-15 14:28:01 +0000367 -5 -4 -3 -2 -1
368
369The first row of numbers gives the position of the indices 0...5 in the string;
370the second row gives the corresponding negative indices. The slice from *i* to
371*j* consists of all characters between the edges labeled *i* and *j*,
372respectively.
373
374For non-negative indices, the length of a slice is the difference of the
375indices, if both are within bounds. For example, the length of ``word[1:3]`` is
3762.
377
378The built-in function :func:`len` returns the length of a string::
379
380 >>> s = 'supercalifragilisticexpialidocious'
381 >>> len(s)
382 34
383
384
385.. seealso::
386
387 :ref:`typesseq`
388 Strings, and the Unicode strings described in the next section, are
389 examples of *sequence types*, and support the common operations supported
390 by such types.
391
392 :ref:`string-methods`
393 Both strings and Unicode strings support a large number of methods for
394 basic transformations and searching.
395
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000396 :ref:`new-string-formatting`
397 Information about string formatting with :meth:`str.format` is described
398 here.
399
Georg Brandl8ec7f652007-08-15 14:28:01 +0000400 :ref:`string-formatting`
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000401 The old formatting operations invoked when strings and Unicode strings are
402 the left operand of the ``%`` operator are described in more detail here.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000403
404
405.. _tut-unicodestrings:
406
407Unicode Strings
408---------------
409
410.. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com>
411
412
413Starting with Python 2.0 a new data type for storing text data is available to
414the programmer: the Unicode object. It can be used to store and manipulate
415Unicode data (see http://www.unicode.org/) and integrates well with the existing
416string objects, providing auto-conversions where necessary.
417
418Unicode has the advantage of providing one ordinal for every character in every
419script used in modern and ancient texts. Previously, there were only 256
420possible ordinals for script characters. Texts were typically bound to a code
421page which mapped the ordinals to script characters. This lead to very much
422confusion especially with respect to internationalization (usually written as
423``i18n`` --- ``'i'`` + 18 characters + ``'n'``) of software. Unicode solves
424these problems by defining one code page for all scripts.
425
426Creating Unicode strings in Python is just as simple as creating normal
427strings::
428
429 >>> u'Hello World !'
430 u'Hello World !'
431
432The small ``'u'`` in front of the quote indicates that a Unicode string is
433supposed to be created. If you want to include special characters in the string,
434you can do so by using the Python *Unicode-Escape* encoding. The following
435example shows how::
436
437 >>> u'Hello\u0020World !'
438 u'Hello World !'
439
440The escape sequence ``\u0020`` indicates to insert the Unicode character with
441the ordinal value 0x0020 (the space character) at the given position.
442
443Other characters are interpreted by using their respective ordinal values
444directly as Unicode ordinals. If you have literal strings in the standard
445Latin-1 encoding that is used in many Western countries, you will find it
446convenient that the lower 256 characters of Unicode are the same as the 256
447characters of Latin-1.
448
449For experts, there is also a raw mode just like the one for normal strings. You
450have to prefix the opening quote with 'ur' to have Python use the
451*Raw-Unicode-Escape* encoding. It will only apply the above ``\uXXXX``
452conversion if there is an uneven number of backslashes in front of the small
453'u'. ::
454
455 >>> ur'Hello\u0020World !'
456 u'Hello World !'
457 >>> ur'Hello\\u0020World !'
458 u'Hello\\\\u0020World !'
459
460The raw mode is most useful when you have to enter lots of backslashes, as can
461be necessary in regular expressions.
462
463Apart from these standard encodings, Python provides a whole set of other ways
464of creating Unicode strings on the basis of a known encoding.
465
466.. index:: builtin: unicode
467
468The built-in function :func:`unicode` provides access to all registered Unicode
469codecs (COders and DECoders). Some of the more well known encodings which these
470codecs can convert are *Latin-1*, *ASCII*, *UTF-8*, and *UTF-16*. The latter two
471are variable-length encodings that store each Unicode character in one or more
472bytes. The default encoding is normally set to ASCII, which passes through
473characters in the range 0 to 127 and rejects any other characters with an error.
474When a Unicode string is printed, written to a file, or converted with
475:func:`str`, conversion takes place using this default encoding. ::
476
477 >>> u"abc"
478 u'abc'
479 >>> str(u"abc")
480 'abc'
481 >>> u"äöü"
482 u'\xe4\xf6\xfc'
483 >>> str(u"äöü")
484 Traceback (most recent call last):
485 File "<stdin>", line 1, in ?
486 UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
487
488To convert a Unicode string into an 8-bit string using a specific encoding,
489Unicode objects provide an :func:`encode` method that takes one argument, the
490name of the encoding. Lowercase names for encodings are preferred. ::
491
492 >>> u"äöü".encode('utf-8')
493 '\xc3\xa4\xc3\xb6\xc3\xbc'
494
495If you have data in a specific encoding and want to produce a corresponding
496Unicode string from it, you can use the :func:`unicode` function with the
497encoding name as the second argument. ::
498
499 >>> unicode('\xc3\xa4\xc3\xb6\xc3\xbc', 'utf-8')
500 u'\xe4\xf6\xfc'
501
502
503.. _tut-lists:
504
505Lists
506-----
507
508Python knows a number of *compound* data types, used to group together other
509values. The most versatile is the *list*, which can be written as a list of
510comma-separated values (items) between square brackets. List items need not all
511have the same type. ::
512
513 >>> a = ['spam', 'eggs', 100, 1234]
514 >>> a
515 ['spam', 'eggs', 100, 1234]
516
517Like string indices, list indices start at 0, and lists can be sliced,
518concatenated and so on::
519
520 >>> a[0]
521 'spam'
522 >>> a[3]
523 1234
524 >>> a[-2]
525 100
526 >>> a[1:-1]
527 ['eggs', 100]
528 >>> a[:2] + ['bacon', 2*2]
529 ['spam', 'eggs', 'bacon', 4]
530 >>> 3*a[:3] + ['Boo!']
531 ['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boo!']
532
Georg Brandl0fcd8822010-03-21 09:17:41 +0000533All slice operations return a new list containing the requested elements. This
534means that the following slice returns a shallow copy of the list *a*::
535
536 >>> a[:]
537 ['spam', 'eggs', 100, 1234]
538
Georg Brandl8ec7f652007-08-15 14:28:01 +0000539Unlike strings, which are *immutable*, it is possible to change individual
540elements of a list::
541
542 >>> a
543 ['spam', 'eggs', 100, 1234]
544 >>> a[2] = a[2] + 23
545 >>> a
546 ['spam', 'eggs', 123, 1234]
547
548Assignment to slices is also possible, and this can even change the size of the
549list or clear it entirely::
550
551 >>> # Replace some items:
552 ... a[0:2] = [1, 12]
553 >>> a
554 [1, 12, 123, 1234]
555 >>> # Remove some:
556 ... a[0:2] = []
557 >>> a
558 [123, 1234]
559 >>> # Insert some:
560 ... a[1:1] = ['bletch', 'xyzzy']
561 >>> a
562 [123, 'bletch', 'xyzzy', 1234]
563 >>> # Insert (a copy of) itself at the beginning
564 >>> a[:0] = a
565 >>> a
566 [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
567 >>> # Clear the list: replace all items with an empty list
568 >>> a[:] = []
569 >>> a
570 []
571
572The built-in function :func:`len` also applies to lists::
573
Georg Brandl87426cb2007-11-09 13:08:48 +0000574 >>> a = ['a', 'b', 'c', 'd']
Georg Brandl8ec7f652007-08-15 14:28:01 +0000575 >>> len(a)
Georg Brandl87426cb2007-11-09 13:08:48 +0000576 4
Georg Brandl8ec7f652007-08-15 14:28:01 +0000577
578It is possible to nest lists (create lists containing other lists), for
579example::
580
581 >>> q = [2, 3]
582 >>> p = [1, q, 4]
583 >>> len(p)
584 3
585 >>> p[1]
586 [2, 3]
587 >>> p[1][0]
588 2
589 >>> p[1].append('xtra') # See section 5.1
590 >>> p
591 [1, [2, 3, 'xtra'], 4]
592 >>> q
593 [2, 3, 'xtra']
594
595Note that in the last example, ``p[1]`` and ``q`` really refer to the same
596object! We'll come back to *object semantics* later.
597
598
599.. _tut-firststeps:
600
601First Steps Towards Programming
602===============================
603
604Of course, we can use Python for more complicated tasks than adding two and two
605together. For instance, we can write an initial sub-sequence of the *Fibonacci*
606series as follows::
607
608 >>> # Fibonacci series:
609 ... # the sum of two elements defines the next
610 ... a, b = 0, 1
611 >>> while b < 10:
Georg Brandl35f88612008-01-06 22:05:40 +0000612 ... print b
613 ... a, b = b, a+b
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000614 ...
Georg Brandl8ec7f652007-08-15 14:28:01 +0000615 1
616 1
617 2
618 3
619 5
620 8
621
622This example introduces several new features.
623
624* The first line contains a *multiple assignment*: the variables ``a`` and ``b``
625 simultaneously get the new values 0 and 1. On the last line this is used again,
626 demonstrating that the expressions on the right-hand side are all evaluated
627 first before any of the assignments take place. The right-hand side expressions
628 are evaluated from the left to the right.
629
630* The :keyword:`while` loop executes as long as the condition (here: ``b < 10``)
631 remains true. In Python, like in C, any non-zero integer value is true; zero is
632 false. The condition may also be a string or list value, in fact any sequence;
633 anything with a non-zero length is true, empty sequences are false. The test
634 used in the example is a simple comparison. The standard comparison operators
635 are written the same as in C: ``<`` (less than), ``>`` (greater than), ``==``
636 (equal to), ``<=`` (less than or equal to), ``>=`` (greater than or equal to)
637 and ``!=`` (not equal to).
638
639* The *body* of the loop is *indented*: indentation is Python's way of grouping
640 statements. Python does not (yet!) provide an intelligent input line editing
641 facility, so you have to type a tab or space(s) for each indented line. In
642 practice you will prepare more complicated input for Python with a text editor;
643 most text editors have an auto-indent facility. When a compound statement is
644 entered interactively, it must be followed by a blank line to indicate
645 completion (since the parser cannot guess when you have typed the last line).
646 Note that each line within a basic block must be indented by the same amount.
647
648* The :keyword:`print` statement writes the value of the expression(s) it is
649 given. It differs from just writing the expression you want to write (as we did
650 earlier in the calculator examples) in the way it handles multiple expressions
651 and strings. Strings are printed without quotes, and a space is inserted
652 between items, so you can format things nicely, like this::
653
654 >>> i = 256*256
655 >>> print 'The value of i is', i
656 The value of i is 65536
657
658 A trailing comma avoids the newline after the output::
659
660 >>> a, b = 0, 1
661 >>> while b < 1000:
662 ... print b,
663 ... a, b = b, a+b
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000664 ...
Georg Brandl8ec7f652007-08-15 14:28:01 +0000665 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
666
667 Note that the interpreter inserts a newline before it prints the next prompt if
668 the last line was not completed.