How do you write multiple lines in python code?
Just like C, you can break a long line into multiple short lines. But in Python, if I do this, there will be an indent error... Is it possible?
asked Nov 13, 2010 at 12:17
1 From PEP 8 - Style Guide for Python Code:
Example of implicit line continuation:
On the topic of line breaks around a binary operator, it goes on to say:
Example of explicit line continuation:
answered Nov 13, 2010 at 12:20
Darin DimitrovDarin Dimitrov 1.0m266 gold badges3252 silver badges2911 bronze badges 3 There is more than one way to do it. 1). A long statement:
2). Using parenthesis:
3). Using
Quoting PEP8:
answered Nov 13, 2010 at 12:26
user225312user225312 121k66 gold badges167 silver badges181 bronze badges 6 If you want to assign a long string to variable, you can do it as below:
Do not add any comma, or you will get a tuple which contains many strings!
answered Apr 27, 2016 at 7:17
acgtyrantacgtyrant 1,6811 gold badge16 silver badges24 bronze badges 3 It works in Python too:
answered Nov 13, 2010 at 12:20
AbyxAbyx 11.8k5 gold badges41 silver badges75 bronze badges When trying to enter continuous text (say, a query) do not put commas at the end of the line or you will get a list of strings instead of one long string:
kinda like that. There is
a comment like this from answered Jul 6, 2016 at 13:11
kotbegkotbeg 1891 silver badge4 bronze badges 1 DB related code looks easier on the eyes in multiple lines, enclosed by a pair of triple quotes:
than the following one giant long line:
answered Sep 20, 2017 at 15:40
2 As far as I know, it can be done. Python has
implicit line continuation (inside parentheses, brackets, and strings) for triple-quoted strings (
answered Nov 13, 2010 at 12:22
1 |