我想将一个变量直接放在一个字符串旁边

时间:2017-06-21 09:32:37

标签: python

目前我的代码看起来像这样:

Unexpected Error 70 - Permission Denied

然后shell看起来像这样:

Word.ActiveDocument.Saved

但我希望它说:

print ("walk into your room")
length = int (input("What is the length of the left hand wall? "))
width = int (input("What is the length of front wall? "))
area = length*width
print (area,"cm² is the area of your room")

因此“1794”和“cm²”之间没有空格

2 个答案:

答案 0 :(得分:2)

使用字符串格式:

print("{}cm² is the area of your room".format(area))

您也可以使用字符串连接,但大多数情况下首选格式:

print(str(area) + "cm² is the area of your room")

答案 1 :(得分:0)

Python 3' print()函数有一个名为sep的参数,专门为此设计:

print(str(area),"cm² is the area of your room",sep="")

默认情况下,sep设置为" ",以便在您要打印的每两个元素之间显示一个空格。如果要在Python 2.x中使用print()函数,可以执行以下操作:

from __future__ import print_function

# your code

print(str(area),"cm² is the area of your room",sep="")

当然,您可能希望先形成一个字符串,然后将其打印出来:

print(str(area)+"cm² is the area of your room")

相关文档: