【问题标题】:An empty line is added at the end, you need to make n-the number of lines最后加了一个空行,需要使n-行数
【发布时间】:2022-12-21 02:46:27
【问题描述】:

Python。有必要使 n-行数。我成功了,但是最后添加了一个空行,我该如何删除它?

a = int(input())
phrase = "Hello!"
final = f"{phrase} \n"
print (final * a)

【问题讨论】:

    标签: python


    【解决方案1】:

    不要用换行符重复字符串,而是创建一个列表并加入换行符:

    a = int(input())
    phrase = "Hello!"
    
    phrases = [phrase] * a
    print("
    ".join(phrases))
    

    【讨论】:

      【解决方案2】:

      您的代码将如下所示:

      a = int(input())
      phrase = "Hello!"
      final =(f"{phrase} 
      ",end="")
      

      【讨论】:

        猜你喜欢
        • 2017-09-11
        • 2016-01-20
        • 2019-07-19
        • 1970-01-01
        • 2018-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-15
        相关资源
        最近更新 更多