Python 如何将字符串和变量分配给变量
我不完全确定问题是什么,但我需要这个来打印我的鸟对象 这是python给我的错误:Python 如何将字符串和变量分配给变量,python,string,variables,object,Python,String,Variables,Object,我不完全确定问题是什么,但我需要这个来打印我的鸟对象 这是python给我的错误: class Bird: def __init__(self): self.noise = "chirping" self.beak = "closed" self.wings = "closed" def __str__(self): msg = "The bird is ", self.noise "its beak is ",
class Bird:
def __init__(self):
self.noise = "chirping"
self.beak = "closed"
self.wings = "closed"
def __str__(self):
msg = "The bird is ", self.noise "its beak is ",
self.beak "its wings are ", self.wings
return msg
mybird = Bird()
print(mybird)
回溯(最近一次呼叫最后一次):
文件“D:/000-Python Coding/Practice/bird object program.py”,第10行,在
印刷品(mybird)
TypeError:\uuuuu str\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
您可以添加+
字符串:
Traceback (most recent call last):
File "D:/000- Python-Coding/Practice/ bird object program.py", line 10, in <module>
print(mybird)
TypeError: __str__ returned non-string (type tuple)
您可以添加
+
字符串:
Traceback (most recent call last):
File "D:/000- Python-Coding/Practice/ bird object program.py", line 10, in <module>
print(mybird)
TypeError: __str__ returned non-string (type tuple)
使用它连接字符串:
msg = "The bird is " + self.noise + " its beak is " + self.beak + " its wings are " + self.wings
所以看起来是这样的:
msg = "The bird is " + self.noise + "its beak is " + self.beak + "its wings are " + self.wings
输出:
class Bird:
def __init__(self):
self.noise = "chirping"
self.beak = "closed"
self.wings = "closed"
def __str__(self):
msg = "The bird is " + self.noise + "its beak is " + self.beak + "its wings are " + self.wings
return msg
mybird = Bird()
print(mybird)
使用它连接字符串:
msg = "The bird is " + self.noise + " its beak is " + self.beak + " its wings are " + self.wings
所以看起来是这样的:
msg = "The bird is " + self.noise + "its beak is " + self.beak + "its wings are " + self.wings
输出:
class Bird:
def __init__(self):
self.noise = "chirping"
self.beak = "closed"
self.wings = "closed"
def __str__(self):
msg = "The bird is " + self.noise + "its beak is " + self.beak + "its wings are " + self.wings
return msg
mybird = Bird()
print(mybird)
如错误消息所述:
The bird is chirpingits beak is closedits wings are closed
您正在构建元组而不是字符串,因此我建议使用字符串格式,如下所示:
TypeError: __str__ returned non-string (type tuple)
如错误消息所述:
The bird is chirpingits beak is closedits wings are closed
您正在构建元组而不是字符串,因此我建议使用字符串格式,如下所示:
TypeError: __str__ returned non-string (type tuple)
告诉你一切
TypeError: __str__ returned non-string (type tuple)
上面用逗号分隔会创建一个元组(item1,item2,…)
您需要的是字符串与+
的连接,或者如其他答案所示的字符串格式
告诉你一切
TypeError: __str__ returned non-string (type tuple)
上面用逗号分隔会创建一个元组(item1,item2,…)
您需要的是与
+
的字符串连接或其他答案中显示的字符串格式。问题在于msg
不是str
(字符串),而是元组。事实上:
msg = "The bird is ", self.noise "its beak is ", self.beak "its wings are ", self.wings
是以逗号分隔的表达式列表,Python将其解释为:
msg = "The bird is ", self.noise "its beak is ", self.beak "its wings are ", self.wings
您可能希望附加可以使用plus(+
)运算符执行的部分:
msg = ("The bird is ", self.noise "its beak is ", self.beak "its wings are ", self.wings)
或者您可以使用:
问题在于
msg
不是str
(字符串),而是元组。事实上:
msg = "The bird is ", self.noise "its beak is ", self.beak "its wings are ", self.wings
是以逗号分隔的表达式列表,Python将其解释为:
msg = "The bird is ", self.noise "its beak is ", self.beak "its wings are ", self.wings
您可能希望附加可以使用plus(+
)运算符执行的部分:
msg = ("The bird is ", self.noise "its beak is ", self.beak "its wings are ", self.wings)
或者您可以使用:
您需要从
函数返回字符串:
msg = "The bird is %s its beak is %s its wings are "%(self.noise,self.beak,self.wings)
noise=“啁啾声”
bick=“关闭”
wings=“关闭”
msg=“鸟是”,噪音,“它的喙是”,喙,“它的翅膀是”,翅膀
类型(味精)#
msg=“鸟是”+噪音+”它的喙是”+喙+”它的翅膀是”+翅膀
类型(味精)#
您需要从\uuu str\uuu
函数返回字符串:
msg = "The bird is %s its beak is %s its wings are "%(self.noise,self.beak,self.wings)
noise=“啁啾声”
bick=“关闭”
wings=“关闭”
msg=“鸟是”,噪音,“它的喙是”,喙,“它的翅膀是”,翅膀
类型(味精)#
msg=“鸟是”+噪音+”它的喙是”+喙+”它的翅膀是”+翅膀
类型(味精)#
Oh。。我想我试过了,但谢谢,它很管用。。我想我试过了,但谢谢你,它能起作用。或者,也可以选择msg=“鸟是{}它的嘴是{}它的翅膀是{}”。格式(self.noise,self.bike,self.wings)
@Shiva:是的。我提供了一个链接,指向一个致力于Python字符串格式化的网站,这样OP可以阅读更多关于它的信息,并决定如何格式化字符串。或者,也可以选择msg=“鸟是{}它的喙是{}它的翅膀是{}”。格式化(self.noise,self.bike,self.wings)
@Shiva:是的。我提供了一个链接,指向一个专门讨论Python字符串格式的网站,这样OP可以阅读更多关于它的信息,并决定如何格式化字符串。