I use Black and Flake8 for formatting my python codes. While I'm very happy with the uncompromised formatting style of black, I just wish it would do something about spurious vertical spaces in the code. There is an excerpt about it in Black's documentation:
Black avoids spurious vertical whitespace. This is in the spirit of PEP 8 which says that in-function vertical whitespace should only be used sparingly.
Black will allow single empty lines inside functions, and single and double empty lines on module level left by the original editors, except when they’re within parenthesized expressions. Since such expressions are always reformatted to fit minimal space, this whitespace is lost.
It will also insert proper spacing before and after function definitions. It’s one line before and after inner functions and two lines before and after module-level functions and classes. Black will not put empty lines between function/class definitions and standalone comments that immediately precede the given function/class.
However from my experience [on black v19.3b0], it doesn't alter vertical spaces in control flow case like this one:
a = 9
if a < 5:
print['The number is smaller than 5']
elif a > 5:
print['The number is larger than 5']
else:
print['Idk what do you want!!']
Sometimes I write it like this
a = 9
if a < 5:
print['The number is smaller than 5']
elif a > 5:
print['The number is larger than 5']
else:
print['Idk what do you want!!']
The second example has all sorts of vertical space inconsistencies and it looks horrible. Black doesn't format it. How would you fix this without manually changing the line spaces?
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In this article, we will learn about how to print space or multiple spaces in the Python programming language. Spacing in Python language is quite simple than other programming language. In C languages, to print 10 spaces a loop is used while in python no loop is used to print number of spaces.
Following are the example of the print spaces:
Example 1: A simple way to print spaces
Python3
print
[
"GeeksForGeeks"
]
print
[
' '
]
print
[
" "
]
print
[
"Geeks For Geeks"
]
Output:
GeeksForGeeks Geeks For Geeks
Example 2: Printing spaces between two values while printing in a single print statement.
Python3
x
=
1
y
=
2
print
[
"x:"
,x]
print
[
"y:"
,y]
print
[x,
"+"
,y,
"="
,x
+
y]
Output:
x: 1 y: 2 1 + 2 = 3
Example 3: Print multiple spaces between two values.
Python3
print
[
"Geeks"
+
" "
+
"For"
+
" "
+
"Geeks"
]
print
[
"Geeks"
,
"For"
,
"Geeks"
]
print
[
"Geeks"
+
" "
*
3
+
"For"
+
" "
*
3
+
"Geeks"
]
print
[
"Geeks"
+
" "
*
5
+
"For"
+
" "
*
10
+
"Geeks"
]
Output:
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Printing the list has been dealt many times. But sometimes we need a different format to get the output of list. This also has application in
getting a transpose of matrix. Printing list vertically also has application in web development. Lets discuss certain ways in which this task can be achieved.
Method #1 : Using Naive Method
The naive method can be used to print the list vertically vis. using the loops and printing each index element of each list successively will help us achieve this task.
test_list
=
[[
1
,
4
,
5
], [
4
,
6
,
8
], [
8
,
3
,
10
]]
print
[
"The original list is : "
+
str
[test_list]]
for
i
in
range
[
len
[test_list]]:
for
x
in
test_list:
print
[x[i], end
=
' '
]
print
[]
Output :
The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 10
Method #2 : Using zip[]
Using zip function, we map the elements at respective index to one other and after that print each of them. This performs the task of vertical printing.
test_list
=
[[
1
,
4
,
5
], [
4
,
6
,
8
], [
8
,
3
,
10
]]
print
[
"The original list is : "
+
str
[test_list]]
for
x, y, z
in
zip
[
*
test_list]:
print
[x, y, z]
Output :
The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]] 1 4 8 4 6 3 5 8 10