[Tutor] Closing triple quotation marks.
Steven D'Aprano
steve at pearwood.info
Sat Jun 18 14:37:38 CEST 2011
Lisi wrote:
[...]
> Fair enough. the closing quotation marks are not there.
>
> But when they _are_ there, i.e. when that stanza reads:
>
> target.write """
> line1\nline2\nline3\n
> """
This is not the problem, but I just thought I'd mention that it's a bit
silly to go to the trouble of using newline escape characters inside a
triple-quoted string! You can do it if you want, but this would be more
naturally written as:
"""
line 1
line 2
line 3
"""
Now, on to your actual error:
> I get:
>
> lisi at Tux:~/Python/LearnPythonTheHardWay$ python extra-credit_16a.py
> learning.txt
> File "extra-credit_16a.py", line 38
> """
> ^
> SyntaxError: invalid syntax
This has nothing to do with the triple quote marks. Simplify the code by
shrinking the text inside the quotes to a single line, and you get:
target.write "..."
and you will get the same SyntaxError. Can you see the problem? No
brackets! You need to include parentheses to call the write method:
target.write("...")
Then you can expand the string to use a triple-quote:
target.write("""
line 1
line 2
line 3
""")
and all should be good.
--
Steven
More information about the Tutor
mailing list