- 给你一个字符串
s
,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。1 - 示例
# 示例 1
输入:s = "Hello"
输出:"hello"
# 示例 2
输入:s = "here"
输出:"here"
# 示例 3
输入:s = "LOVELY"
输出:"lovely"
- 解法1
class Solution(object):
def toLowerCase(self, s):
"""
:type s: str
:rtype: str
"""
return s.lower()
- 解法2
class Solution(object):
def toLowerCase(self, s):
"""
:type s: str
:rtype: str
"""
lower_str = ""
for ch in s:
if ord(ch) in range(65, 91):
num = ord(ch) + 32
ch = chr(num)
lower_str += ch
else:
lower_str += ch
return lower_str
- 扩展:
ord()
:字符转化为数字
chr()
:数字转化为字符
title()
:所有大写字母转换为小写字母
lower()
:所有大写字母转换为小写字母
upper()
:所有小写字母转换为大写字母
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/to-lower-case
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 ↩︎