繁体   English   中英

与 Python 字符串大写相反

[英]Opposite to capitalize for Python strings

如果我想让一个字符串以大写字母开头,我会

"hello world".capitalize()  # produces 'Hello world'

但是,我需要完全相反:我需要使字符串以小写字母开头,因此是这样的:

"Hello world".decapitalize()  # produces 'hello world'
"HELLO WORLD".decapitalize()  # produces 'hELLO WORLD'

我们在Python中有这样的函数/方法,还是需要从头开始编码?

python2和python3都没有这样的function,所以要自己加代码:

def decapitalize(s):
    if not s:  # check that s is not empty string
        return s
    return s[0].lower() + s[1:]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM