如何将字符串的第一个字符小写? [英] How to downcase the first character of a string?

查看:33
本文介绍了如何将字符串的第一个字符小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个函数可以将字符串大写,我希望能够更改字符串的第一个字符以确保它是小写的.

There is a function to capitalize a string, I would like to be able to change the first character of a string to be sure it will be lowercase.

我如何在 Python 中做到这一点?

How can I do that in Python?

推荐答案

One-liner 处理空字符串和 None:

One-liner which handles empty strings and None:

func = lambda s: s[:1].lower() + s[1:] if s else ''

>>> func(None)
>>> ''
>>> func('')
>>> ''
>>> func('MARTINEAU')
>>> 'mARTINEAU'

这篇关于如何将字符串的第一个字符小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆