Lowest level text processing (3.2.1-3.2.2)
Now at chapter 3.2.1 of the whale book.
This part is going back to the basic of text processing. We need to escape ('\') or double quotation(") in case single quotations (') are included in the text.
>>> monty = 'Monty Python' >>> monty 'Monty Python' >>> circus = "Monty Python's Flying Circus" >>> circus "Monty Python's Flying Circus" >>> circus = 'Mongy Python\'s Flying Circus' >>> circus "Mongy Python's Flying Circus" >>> circus2 = 'Monty Python's Flying Circus' File "<stdin>", line 1 circus2 = 'Monty Python's Flying Circus' ^ SyntaxError: invalid syntax >>>
How can I treat if double quotations are used in the text?
SyntaxError: invalid syntax >>> special text = 'When you need to quote something from other document, you should use double quotation(") to clarify what did you quote.' File "<stdin>", line 1 special text = 'When you need to quote something from other document, you should use double quotation(") to clarify what did you quote.' ^ SyntaxError: invalid syntax >>> special text = 'When you need to quote something from other document, you should use double quotation(\") to clarify what did you quote.' File "<stdin>", line 1 special text = 'When you need to quote something from other document, you should use double quotation(\") to clarify what did you quote.' ^ SyntaxError: invalid syntax >>>
How to process multiple lines.
>>> couplet = "Shall I compare thee to a Summer's day?"\ ... "Thou are more lovely and more temperate:" >>> print couplet Shall I compare thee to a Summer's day?Thou are more lovely and more temperate: >>> couplet = ("Rough winds do shake the darling buds of May," ... "And Summer's lease hath all too short a date:") >>> print couplet Rough winds do shake the darling buds of May,And Summer's lease hath all too short a date: >>>
Two sentences are concatenated into single line. If need to change lines, use triple quotation like this.
>>> couplet = """Shall I compare thee to a Summer's day? ... Thou are more lovely and more temperate:""" >>> print couplet Shall I compare thee to a Summer's day? Thou are more lovely and more temperate: >>> couplet = '''Shall I compare thee to a Summer's day? ... Thou are more lovely and more temperate:''' >>> print couplet Shall I compare thee to a Summer's day? Thou are more lovely and more temperate: >>>
This part is just repeating same thing as chapter 1.
>>> 'very' + 'very' + 'very' 'veryveryvery' >>> 'very' * 3 'veryveryvery' >>> >>> a = [1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1] >>> b = [' ' * 2 * (7 - i) + 'very' * i for i in a] >>> for line in b: ... print line ... very veryvery veryveryvery veryveryveryvery veryveryveryveryvery veryveryveryveryveryvery veryveryveryveryveryveryvery veryveryveryveryveryvery veryveryveryveryvery veryveryveryvery veryveryvery veryvery very >>> >>> 'very' - 'y' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for -: 'str' and 'str' >>> 'very' / 2 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for /: 'str' and 'int' >>> ||< In Python, you can get the value of the variables by just entering the name. If the type of the variable is str, the output to be with quotation('). On the other hand, those quotations are not displayed if you use <strong>print</strong>. (3.2.2) >|python| >>> monty 'Monty Python' >>> print monty Monty Python >>> >>> grail = 'Holy Grail' >>> print monty + grail Monty PythonHoly Grail >>> print monty, grail Monty Python Holy Grail >>> print monty, "and the", grail Monty Python and the Holy Grail >>>