简体   繁体   English

\\n 在函数的 return 语句中打印 \\n 而不是 python 中的新行

[英]\n in return statement of function prints \n instead of new line in python

Consider the below python code.I am very new to python.Please help me with this.考虑下面的python代码。我对python很陌生。请帮我解决这个问题。 this function returns '\\n56' but i need --new line-- 56此函数返回 '\\n56' 但我需要 --new line-- 56

def fun_ret(num):
    return '\n'+str(num)
if __name__=='__main__':
    a=fun_ret(56)

I have gone through similar posts but it was all about print statement我看过类似的帖子,但都是关于打印声明的

Actual scenario is to pass the doctest of method str in a class.实际场景是在类中传递方法str的 doctest。

def __str__(self):
    """(Maze)-->str
    The parameter represents a maze. Return a string representation of the maze 
    >>> maze=Maze([['#', '#', '#', '#', '#', '#', '#'],['#', 'J', '.', '.', 'P', '.', '#'], ['#', '.', '#', '#', '#', '.', '#'],['#', '.', '.', '@', '#', '.', '#'],['#', '@', '#', '.', '@', '.', '#'], ['#', '#', '#', '#', '#', '#', '#']], Rat('J', 1, 1),Rat('P', 1, 4))
    >>> str(maze)
    "#######
    #J..P.#
    #.###.#
    #..@#.#
    #@#.@.#
    #######
    J at (1, 1) ate 0 sprouts.
    P at (1, 4) ate 0 sprouts."
    """
    result=''
    for outer_list in self.maze_content:
        for inner_list in outer_list:
            result +='{0}'.format(str(inner_list))
        result+='\n'
    result += '\n'+'{0} at ({1}, {2}) ate {3} sprouts.'.format(self.rat_1.symbol,self.rat_1.row,self.rat_1.col,self.rat_1.num_sprouts_eaten)
    result += '\n'+('{0} at ({1}, {2}) ate {3} sprouts.'.format(self.rat_2.symbol,self.rat_2.row,self.rat_2.col,self.rat_2.num_sprouts_eaten))
    return result


if __name__=='__main__':
    import doctest
    doctest.testmod()

Are you working in a line-oriented command shell?您是否在面向行的命令 shell 中工作? If so, I can see your confusion.如果是这样,我可以看到你的困惑。 A typical command shell has a special routine for simply displaying a variable value, one that does not convert output formatting characters.典型的命令 shell 有一个特殊的例程,用于简单地显示变量值,转换输出格式字符。 For instance, the default Python interactive shell gives us this:例如,默认的 Python 交互式 shell 为我们提供了以下信息:

>>> def fun_ret(num):
...     return '\n'+str(num)
... 
>>> a=fun_ret(56)
>>> a
'\n56'
>>> print(a)

56
>>> 

Note the difference: the simple a command carefully displays the string value of the variable, expanding the visual short-hand for various control characters.注意区别:简单的a命令仔细显示变量的字符串值,扩展了各种控制字符的可视化简写。 The print command applies the standard formatter, which is where the \\n character is turned into an actual "new line" operation. print命令应用标准格式化程序,这是将\\n字符转换为实际“换行”操作的地方。

Does that help clear up the problem?这有助于解决问题吗?

I know this is a really old question at this point but I found it while trying to solve a similar problem myself (I was doing a challenge in a course I was taking in which I had to print a string containing multiple lines out of a dictionary of strings).我知道这是一个非常古老的问题,但我在自己尝试解决类似问题时发现了它(我正在参加的一门课程中做了一个挑战,我必须从字典中打印一个包含多行的字符串字符串)。 So, since some of the comments lead me to the answer, I'd like to share it here in case someone else has the same problem later.因此,由于某些评论使我找到了答案,因此我想在这里分享,以防其他人以后遇到同样的问题。

Instead of returning, in OP's case, just the string:而不是返回,在 OP 的情况下,只是字符串:

def fun_ret(num):
    return '\n'+str(num)

you can return a print statement, like so:您可以返回打印语句,如下所示:

def fun_ret(num):
    return print('\n'+str(num))

This wouldn't necessarily work if you potentially need to return that function into just a variable somewhere, in which case you'd probably be better off calling it in a print statement:如果您可能需要将该函数返回到某个地方的一个变量中,则这不一定有效,在这种情况下,您最好在打印语句中调用它:

print(fun_ret(num)) 

instead.反而。

I'm not sure if this will help with the actual situation specified by OP (pretty new to Python myself), but it will cause the function call to print a new line, rather than just return the value, if that's what someone needs for their use case.我不确定这是否对 OP 指定的实际情况有帮助(我自己对 Python 还很陌生),但它会导致函数调用打印一个新行,而不仅仅是返回值,如果有人需要的话他们的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM