文章内容

2023/7/17 1:24:11,作 者: 黄兵

Python 大写转换

在 Python 中,有几种方法可以将字符串转换为大写。 一种方法是使用 upper() 方法。 upper() 方法将字符串中的所有字母都转换为大写。 例如,以下代码将字符串 'hello world' 转换为 'HELLO WORLD'

str = 'hello world' print(str.upper())

另一种方法是使用 str.swapcase() 方法。 str.swapcase() 方法将字符串中的所有大写字母都转换为小写,所有小写字母都转换为大写。 例如,以下代码将字符串 'hello world' 转换为 'hELLO wORLD'

str = 'hello world' print(str.swapcase())

最后,你也可以使用 str.capitalize() 方法。 str.capitalize() 方法将字符串中的第一个字母转换为大写,其余字母都转换为小写。 例如,以下代码将字符串 'hello world' 转换为 'Hello world'

str = 'hello world' print(str.capitalize())

如果有任何问题,欢迎下面讨论交流。

其它相关推荐:

1、Python 如何获取字典键值

2、python bisect循环部分列表

3、Python任务调度模块APScheduler

4、python获取for索引

5、TypeError: must be str, not int

分享到:

发表评论

评论列表