Python str upper方法

您所在的位置:网站首页 upper怎么用python Python str upper方法

Python str upper方法

2023-03-14 13:24| 来源: 网络整理| 查看: 265

目录

描述

语法

参数

返回值

使用示例

注意事项

1. 字符串原本存在大写字母时

2. 字符串存在非字母字符

描述

upper()方法是Python的字符串方法,它将字符串中的小写字母转换成大写字母,并返回转换后的字符串。

语法 str.upper()

参数

upper()方法没有参数

返回值

返回一个修改后的新字符串

使用示例

upper()方法将字符串中的非大写字母转换成大写,并返回新的字符串。

>>> demo = "Welcome to Beijing." >>> id(demo) 4467791816 >>> result = demo.upper() >>> result 'WELCOME TO BEIJING.' >>> id(result) 4467791960

注意事项 1. 字符串原本存在大写字母时

在使用upper()方法之前字符串原本就存在大写字母时,upper()方法将保留大写字母,并将小写字母转换成大写字母。

>>> demo = "Golang is a new language." >>> demo 'Golang is a new language.' >>> demo.upper() 'GOLANG IS A NEW LANGUAGE.'

2. 字符串存在非字母字符

upper()不会处理非字母字符,更不会报错。

>>> demo = "??" >>> demo '??' >>> demo.upper() '??'



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3