你如何打印python中的空白行?

时间:2013-03-04 06:31:16

标签: python

我无法找到任何相关内容 我想知道如何使用可以打印20个空行的函数(例如clear_screen) 我的程序的最后一行应该是对clear_screen的调用。

我的代码的开头是:

def new_line():
    print
def three_lines():
    new_line()
    new_line()
    new_line()
def nine_lines():
    three_lines()
    three_lines()
    three_lines()
print " "
nine_lines()
print " "

打印功能有效,但不适用clear_screen(),这就是我需要的工作 如果有人可以帮助我或提出任何建议,那就太棒了,谢谢。

2 个答案:

答案 0 :(得分:3)

我认为没有一种跨平台的方式。因此,不是依赖os.*,而是可以使用以下内容

print("\n"*20)

答案 1 :(得分:3)

您的clear_screen可以

  1. os.system基于

    def clear_screen():
        import os
        os.system( [ 'clear', 'cls' ][ os.name == 'nt' ] )
    

    适用于unix和Windows 资料来源:Here

  2. 基于换行

    def clear_screen():
        print '\n'*19 # print creates it's own newline
    

  3. 根据您的评论,您的代码似乎是

    def new_line():
        print
    def three_lines():
        new_line()
        new_line()
        new_line()
    def nine_lines():
        three_lines()
        three_lines()
        three_lines()
    print " "
    nine_lines()
    print " "
    

    它会起作用,确实 但是如果print '\n'*8能够做同样的事情,为什么还要这么长的代码呢?

    速度测试
    即使你没有速度限制,这里有一些速度统计数据,每次运行100次

    os.system function took 2.49699997902 seconds.
    '\n' function took 0.0160000324249 seconds.
    Your function took 0.0929999351501 seconds.