如何在 Python 中打印变量和字符串,中间没有空格 [英] How to print a variable and a string in Python without a space between

查看:123
本文介绍了如何在 Python 中打印变量和字符串,中间没有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

a = 60

print(a, ": string of text")

这会打印 "60 : string of text"

如果有意义的话,我希望它打印 "60: String of text"60 后面没有空格

I would like it to print "60: String of text" without the space after the 60 if that makes sense

有什么想法吗?

推荐答案

有几种方法可以删除空格分隔符.幸运的是 print() 默认只在字段之间插入一个空格.来自 print() 文档:

There are several ways to remove the space delimiter. Fortunately print() only inserts a space between fields by default. From the print() doc:

"sep:值之间插入的字符串,默认为空格."

所以我的首选方法是使用空字符串:

So my preferred way would be to use an empty string:

print(a, ": string of text", sep="")

这篇关于如何在 Python 中打印变量和字符串,中间没有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆