简体   繁体   English

删除逗号或撇号前面的空格 - Python

[英]remove white space in front of comma or apostrophe - Python

How do I remove the white space in front of an apostrophe or comma so the sentence in print() will read better?如何删除撇号或逗号前面的空格,以便 print() 中的句子读起来更好?

CTyear = 2020
Name1 = "Dave"
print("Hello",Name1,",","its the year",CTyear,".")

"Hello Dave , its the year 2020 ."<- how do I remove the white space in front of the apostrophe and comma? “你好戴夫,现在是 2020 年。”<- 如何删除撇号和逗号前面的空格? Thanks.谢谢。

Use string formatting:使用字符串格式:

CTyear = 2020
Name1 = "Dave"
print(f"Hello {Name1}, it's the year {CTyear}.")

Output输出

Hello Dave, it's the year 2020.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM