I am trying to print out Python path folders using this:
import sys
print sys.path
The output is like this:
>>> print sys.path
['.', '/usr/bin', '/home/student/Desktop', '/home/student/my_modules', '/usr/lib/pyth
on2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/pyth
on2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-pack
ages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/
usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/
python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/p
ython2.6/dist-packages/wx-2.8-gtk2-unicode']
How do I print them into separate lines so I can parse them properly?
It should be like this:
/usr/bin
/home/student/Desktop
/home/student/my_modules
etc
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
We have already seen the basic use of print function
previous article. Now, let’s see how to use print function for multi-line printing. This can easily be done using multiline string i.e. three single quotes ''' Geeksforgeeks '''
.
Let’s see different examples to see the demonstration for the same.
Example #1:
Output:
======================================= | | | | | GeeksforGeeks | | | | | =======================================
Example #2:
Output:
list.head second third | | | | | | +----+------+ +----+------+ +----+------+ | 1 | None | | 2 | None | | 3 | None | +----+------+ +----+------+ +----+------+
In this article, we are going to understand the concept of Multi-Line statements in the Python programming language.
Statements in Python:
In Python, a statement is a logical command that a Python interpreter can read and carry out. It might be an assignment statement or an expression in Python.
Multi-line Statement in Python:
In Python, the statements are usually written in a single line and the last character of these lines is newline. To extend the statement to one or more lines we can use braces {}, parentheses [], square [], semi-colon “;”, and continuation character slash “\”. we can use any of these according to our requirement in the code. With the line continuation character, we can explicitly divide a long statement into numerous lines [\].
Code:
Python3
g
=
"geeks\
for
\
geeks"
print
[g]
In the above code if we do not use the continuation characters the code will give unterminated string literal error.
Output:
geeksforgeeks
Line continuation are divided into two different ways:
- Explicit line continuation
- Implicit line continuation
Explicit line continuation:
In this type of multi-line statement, we will be using the line continuation character [\] to split a statement into multiple lines.
Example:
In this example, we are initializing the text, and the mathematical expression using the ‘\’ sign which is the explicit line continuation to continue the same line in the multiple lines in python programming.
Python3
text
=
"A Computer Science portal\
for
geeks. It contains well written, well \
thought
and
well explained \
computer science
and
programming \
articles"
print
['\n Initializing a text using\
the Explicit multi
-
line statement', text]
add
=
50
+
\
40
-
\
52
print
['\n Initializing a mathematical expression\
using the Explicit multi
-
line statement', add]
Output:
Initializing a text using the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well thought and well explained computer science and programming articles Initializing a mathematical expression using the Explicit multi-line statement 38
Implicit line continuation:
In this type of multi-line statement, Implicit line continuation is used when you split a statement using either parentheses [ ], brackets [ ], and braces { }.
Example:
In this example, we are initializing the list and the mathematical expression using the parentheses [ ], brackets [ ], and braces { } sign which is the implicit line continuation to continue the same line in the multiple lines in python programming.
Python3
list
=
[
5
,
4
,
3
,
2
,
1
]
print
['Initializing a
list
using the\
Implicit multi
-
line statement',
list
]
add
=
[
50
+
40
-
52
]
print
['\n Initializing a mathematical expression\
using the Explicit multi
-
line statement', add]
Output:
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 38