AttributeError: 'NoneType' 对象没有属性 'format' [英] AttributeError: 'NoneType' object has no attribute 'format'
本文介绍了AttributeError: 'NoneType' 对象没有属性 'format'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
print ("Hello World")
print ("{} World").format(Hello)
我正在开发我的第一个Hello World"程序,我可以通过使用打印功能和一个简单的字符串文本来让它工作,但是当我尝试使用 .format
时,它给出我的错误:
I'm working on my first "Hello World" program and I can get it to work by using the print function and just a simple string text but when I try to use .format
it gives me the error:
AttributeError: 'NoneType' object has no attribute 'format'
这是说我需要为 .format
初始化一个变量还是我遗漏了什么?
Is this saying that I need to initialize a variable for .format
or am I missing something?
推荐答案
你的括号错了
print("Hello World")
print("{} World".format('Hello'))
注意 - 错误
format
函数是str
的一个属性,所以需要在字符串上调用它- 除非声明,
Hello
是一个字符串,应该是'Hello'
- The
format
function is an attribute ofstr
so it needs to be called on the string - Unless declared,
Hello
is a string and should be'Hello'
对于 Py2,你可以做到
For Py2 you can do
print "{} World".format('Hello')
这篇关于AttributeError: 'NoneType' 对象没有属性 'format'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文