【问题标题】:Want to remove the space between Last name and "," [duplicate]想要删除姓氏和“,”之间的空格[重复]
【发布时间】:2020-12-04 20:41:57
【问题描述】:
print(row["first"], row["last"], ",", "born", row["birth"])

输出:

Ronald Bilius Weasley , born 1980

我想在 Weasley 之后删除空格。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    打印中的 , 会自动在其后显示一个空格,因此您可以像这样防止这种情况:

    print(row["first"], row["last"] + ",", "born", row["birth"])
    

    或者你也可以这样做:

    print("{} {}, born {}".format(row["first"], row["last"], row["birth"]))
    

    【讨论】:

      【解决方案2】:

      您可以像这样使用sep 选项。 sep 用于表示您要如何分离。默认情况下,python 使用空格来表示逗号。您可以通过将 sep 的值重置为 ''

      来更改它
      print(row["first"], " ",row["last"], ", born ", row["birth"], sep = '')
      

      输出:

      Ronald Bilius Weasley, born 1980
      

      有很多方法可以解决您的代码。以上是其中一种方式。这是一个查看一些选项的链接。

      What are the difference between sep and end in print function?

      您可以尝试使用str.join().format() 选项或简单的+ 来连接字符串。

      要了解有关格式打印的更多信息,请参阅此链接。 https://pyformat.info/

      【讨论】:

        猜你喜欢
        • 2014-11-24
        • 2018-08-28
        • 2011-09-02
        • 1970-01-01
        • 2016-11-27
        • 2020-04-26
        • 2023-03-11
        • 1970-01-01
        • 2019-01-16
        相关资源
        最近更新 更多