python upper_Python 3中的模块级string.upper函数在哪里?
如何讓這段代碼在3中運(yùn)行?
請(qǐng)注意,我不是"foo".upper()在字符串實(shí)例級(jí)別詢問(wèn)。import string
try:
print("string module, upper function:")
print(string.upper)
foo = string.upper("Foo")
print("foo:%s" % (foo))
except (Exception,) as e:
raise
輸出2:string module, upper function:
foo:FOO
輸出3:string module, upper function:
Traceback (most recent call last):
File "dummytst223.py", line 70, in
test_string_upper()
File "dummytst223.py", line 63, in test_string_upper
print(string.upper)
AttributeError: module 'string' has no attribute 'upper'
help(string)也不是很有幫助。據(jù)我所知,剩下的唯一功能就是string.capwords。
注意:有點(diǎn)hacky,但這是我的短期解決方法。import string
try:
_ = string.upper
except (AttributeError,) as e:
def upper(s):
return s.upper()
string.upper = upper
總結(jié)
以上是生活随笔為你收集整理的python upper_Python 3中的模块级string.upper函数在哪里?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java 位运算取8位_Java 9 A
- 下一篇: docker 删除image_如何用两个