【问题标题】:Centering string on greeting在问候语上居中字符串
【发布时间】:2021-06-10 23:43:30
【问题描述】:

我正在尝试将这个问候放在主题标签之间。

def setup():
    print("#" * 100)
    welcome = "Welcome to my adventure game!"
    welcome.center(25)
    print(welcome)
    print("#" * 100)

【问题讨论】:

    标签: python string center


    【解决方案1】:

    恕我直言,我不明白你怎么没有意识到这一点。

        welcome = welcome.center(100)
    

    【讨论】:

    • 好吧,我只是一个想学习python的孩子。现在我感觉很糟糕。
    【解决方案2】:

    或者你可以用数学方法来做:

    def setup():
        print("#" * 100)
        welcome = "Welcome to my adventure game!"
        spacer = ' ' * (50 - len(welcome) // 2)
        print(spacer + welcome)
        print("#" * 100)
    

    【讨论】:

      【解决方案3】:

      如果你想用空格以外的东西包围他的欢迎信息,请使用中心的第二个参数。

      print(welcome.center(100, "#"))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        • 1970-01-01
        • 2017-05-23
        相关资源
        最近更新 更多