【问题标题】:how do I separate two strings in python that have been printed using functions?如何在 python 中分隔两个使用函数打印的字符串?
【发布时间】:2015-07-08 04:15:59
【问题描述】:

我创建了一个函数来打印每个字符之间有时间延迟的字符串,但是在打印多个字符串时,它们都打印在同一行上。我将如何分隔线? ('\n' 不起作用)

from time import sleep
import sys

def slowly(text):
    for letters in text:
        print (letters, end=''),
        sys.stdout.flush()
        sleep(0.1)
        if letters == ',':
            sleep(1)

slowly("hello world")
slowly("hello world")

结果

"hello worldhello world"...

【问题讨论】:

    标签: string python-3.x newline


    【解决方案1】:

    在循环后给函数添加换行符,比如

    from time import sleep
    
    def slowly(text):
        for letter in text:
            print(letter, end = '')
            sleep(0.1)
            if letter == ',':
                sleep(1)
        print()    # Print a newline
    
    slowly('hello world')
    slowly('hello world')
    

    这对我有用。

    输出

    hello world
    hello world
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      • 2014-12-13
      • 2023-03-19
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多