How to print list items which contain new line?

后端 未结 4 713
感动是毒
感动是毒 2021-01-29 12:07

These commands:

l = [\"1\\n2\"]    
print(l)

print

[\'1\\n2\']

I want to print

[\'1
2\']
         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-29 12:21

    Only if you are printing the element itself (or each element) and not the whole list:

    >>> a = ['1\n2']
    >>> a
    ['1\n2']
    >>> print a
    ['1\n2']
    >>> print a[0]
    1
    2
    

    When you try to just print the whole list, it prints the string representation of the list. Newlines belong to individual elements so get printed as newlines only when print that element. Otherwise, you will see them as \n.

提交回复
热议问题