练习6:To Lower Case转换为小写

1、题目链接

  https://leetcode.com/problems/to-lower-case/description/

2、题目要求

  将给定的字符串转换为小写

  

3、解答

1 class Solution:
2     def toLowerCase(self, str):
3         """
4         :type str: str
5         :rtype: str
6         """
7         result = str.lower()
8         return result

4、官方解答

  官方无解答,如果不使用自带的方法,可以如下实现

1 class Solution:
2     def toLowerCase(self, str):
3         """
4         :type str: str
5         :rtype: str
6         """
7         return "".join(chr(ord(c) + 32) if ord('A') <= ord(c) <= ord('Z') else c for c in str)

5、注意事项

  从测试情况看ord('A') <= ord(c) <= ord('Z')比'A'<=c<="Z"执行速度快

猜你喜欢

转载自www.cnblogs.com/hzerdudu/p/9509318.html
今日推荐