现在的位置: 首页 > 综合 > 正文

001_012 Python 字符串大小写转化

2018年02月15日 ⁄ 综合 ⁄ 共 345字 ⁄ 字号 评论关闭

代码如下:

#encoding=utf-8
print '中国'

#字符串大小写转化
str ='aBc中国'
ustr =u'aBc中国'

print str.upper()
print str.lower()
print str.capitalize() #第一个大写其余小写
print ustr.upper()
print ustr.lower()
print ustr.capitalize()

#判断是否是第一个大写其余小写 不适用于空字符串和非字母字符串
def isCapitalize(s):
    return s == s.capitalize()

print isCapitalize(str)
print isCapitalize(ustr)

运行结果:

中国
ABC中国
abc中国
Abc中国
ABC中国
abc中国
Abc中国
False
False

抱歉!评论已关闭.