page contents

怎样将字符串中第一个字母大写?

王昭君 发布于 2022-12-23 15:41
阅读 1979
收藏 0
分类:Python开发
4433
Nen
Nen
- 程序员

最简单的方法就是用capitalize()方法。

>>> 'ayushi'.capitalize()‘Ayushi’>>> type(str.capitalize)<class 'method_descriptor'>

不过这也会让其它字母变为大写。

>>> '@yushi'.capitalize()‘@yushi’
请先 登录 后评论
4438
薛飞
薛飞

我会切片  str[0].upper+str[1:-1]

请先 登录 后评论
4451
杨一锴
杨一锴

我想到的一种方法,比如说字符串words='wefEFv'

print(words[0].upper()+words[1:])

我们可以得到'WefEFv'

请先 登录 后评论