DDR爱好者之家 Design By 杰米

①所有字母都转换为大写

# -*- coding:utf-8 -*-

if __name__ == "__main__":
    a = 'hello, world!'
    print(a.upper())输出:


HELLO, WORLD!

②所有字母都转换为小写

# -*- coding:utf-8 -*-

if __name__ == "__main__":
    a = 'HELLO, WORLD!'
    print(a.lower())输出:


hello, world!

③首字母转换成大写, 其余转换成小写

# -*- coding:utf-8 -*-

if __name__ == "__main__":
    a = 'HELLO, WORLD!'
    print(a.capitalize())输出:


Hello, world!


④所有单词的首字母转换成大写, 其余转换成小写

# -*- coding:utf-8 -*-

if __name__ == "__main__":
    a = 'HELLO, WORLD!'
    print(a.title())输出:


Hello, World!

⑤判断所有字母都为大写

# -*- coding:utf-8 -*-

if __name__ == "__main__":
    a = 'HELLO, WORLD!'
    print(a.isupper())
   
    b = 'hello, world!'
    print(b.isupper())输出:


True
False


⑥判断所有字母都为小写

# -*- coding:utf-8 -*-

if __name__ == "__main__":
    a = 'HELLO, WORLD!'
    print(a.islower())
   
    b = 'hello, world!'
    print(b.islower())输出:


False
True


⑦判断所有单词的首字母为大写

# -*- coding:utf-8 -*-

if __name__ == "__main__":
    a = 'HELLO, WORLD!'
    print(a.istitle())
   
    b = 'hello, world!'
    print(b.istitle())
   
    c = 'Hello, World!'
    print(c.istitle())输出:


False
False
True

以上这篇Python 字符串大小写转换的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米