如果我想让一个字符串以大写字母开头,我会这样做
"hello world".capitalize() # produces 'Hello world'
Run Code Online (Sandbox Code Playgroud)
但是,我需要完全相反:我需要使字符串以小写字母开头,因此如下所示:
"Hello world".decapitalize() # produces 'hello world'
"HELLO WORLD".decapitalize() # produces 'hELLO WORLD'
Run Code Online (Sandbox Code Playgroud)
我们在Python中是否有这样的函数/方法,或者需要从头开始编码?
python2和python3中都没有这个函数,所以你必须自己添加代码:
def decapitalize(s):
if not s: # check that s is not empty string
return s
return s[0].lower() + s[1:]
Run Code Online (Sandbox Code Playgroud)