It works in normal python interactive mode:
>>> """1
...
... 2"""
'1\n\n2'
However, the second \n
is gone in iPython
In [4]: """1
...:
...: 2"""
Out[4]: '1\n2'
What's wrong?
To match empty lines, use the pattern ' ^$ '.
Use an if-statement to check if a line is empty Call file. readlines() to return all lines in file . Use a for-loop to iterate through each line. For each line, use an if-statement with the syntax if line == "\n" to check if line contains only a newline character.
In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line. Essentially the occurrence of the “\n” indicates that the line ends here and the remaining characters would be displayed in a new line.
Use print() without any arguments to print a blank line Call print() without any arguments to output a blank line.
Finally I found that it's been solved in the newest version. Here's the committing
The reason is that while IPython
use raw_input
to capture what use type, the \n
is being stripped. And then the string will be append a '\n' later. However, if the string is an empty string, it'll be thrown out. The flow is like:
if not s:
return
s = s+'\n'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With