string.upper(<str>) and <str>.upper() won't execute

一笑奈何 提交于 2019-11-28 12:55:47

Strings are immutable. So functions like str.upper() will not modify str but return a new string.

>>> name = "xyz"
>>> name.upper()
'XYZ'
>>> print name
xyz  # Notice that it's still in lower case.
>>> name_upper = name.upper()
>>> print name_upper
XYZ

So instead of fragment.upper() in your code, you need to do new_variable = fragment.upper()and then use this new_variable.

You're not realizing that strings in Python are immutable and that string methods and operations return new strings.

>>> print 'ppp'.upper()
PPP

String is a immutable object, so when you call

string.upper()

python would make a copy of the string, and when you come back call

print string

, it would be the original string, which is lower case. So when you need its upper case version, you have to say:

print string.upper()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!