Python:删除字符串前的空格

时间:2021-02-21 18:33:10

标签: python

print ("The number is", (roll),'.')

数字是 5。

我想删除'5'和'.'之间的空格

我尝试了几种不同的方法来删除句点前的空格,但我要么得到错误要么得到相同的结果。

3 个答案:

答案 0 :(得分:1)

您需要一种字符串格式化方法或连接方法。以下将给你相同的结果


print (f"The number is, {roll}.")
print ("The number is, {}.".format(roll))
print ("The number is"+' '+ str(roll) +'.')
print ("The number is %s. "%(a))

答案 1 :(得分:1)

您的问题有多种解决方案。我可以推荐f-strings

print ("The number is", (roll),'.')

变成

print(f"The number is {roll}.")

答案 2 :(得分:1)

您的问题有多种解决方案,其中一种解决方案是使用 .format 函数。

print ("The number is {}.".format(roll))

相关问题