Python 字符串大小写转换的简单实例

yipeiwu_com5年前Python基础

①所有字母都转换为大写

# -*- 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 字符串大小写转换的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python3 http提交json参数并获取返回值的方法

如下所示: import json import http.client connection = http.client.HTTPSConnection('spd.aiopos...

初次部署django+gunicorn+nginx的方法步骤

环境 ubuntu16.04 python3.6 django1.11 gunicorn19.7.1 nginx1.10.3 写在前面 其实无论是在部署,还是在其他方面,我们总会遇到一...

Python实现银行账户资金交易管理系统

Python实现银行账户资金交易管理系统

用类和对象实现一个银行账户的资金交易管理, 包括存款、取款和打印交易详情, 交易详情中包含每次交易的时间、存款或者取款的金额、每次交易后的余额。 如: 下面按照要求定义一个账户 Acc...

python实现嵌套列表平铺的两种方法

方法一:使用列表推导式 >>> vec = [[1,2,3],[4,5,6],[7,8,9]] >>> get = [num for elem i...

Python实现Youku视频批量下载功能

Python实现Youku视频批量下载功能

前段时间由于收集视频数据的需要,自己捣鼓了一个YouKu视频批量下载的程序。东西虽然简单,但还挺实用的,拿出来分享给大家。   版本:Python2.7+BeautifulSoup3.2...