Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPython didn't catch the empty line as a `\n`

Tags:

python

ipython

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?

like image 356
waitingkuo Avatar asked May 17 '13 03:05

waitingkuo


People also ask

How do you match an empty line in Python?

To match empty lines, use the pattern ' ^$ '.

How do you check if a line in a file is empty?

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.

How do you add an extra line in Python?

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.

How do you print white lines in Python?

Use print() without any arguments to print a blank line Call print() without any arguments to output a blank line.


1 Answers

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'
like image 111
waitingkuo Avatar answered Oct 16 '22 05:10

waitingkuo