[leetcode] 709. To Lower Case

天蓝Sea

关注

阅读 69

2022-08-12


Description

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

Example 1:

Input: "Hello"
Output: "hello"

Example 2:

Input: "here"
Output: "here"

Example 3:

Input: "LOVELY"
Output: "lovely"

分析

题目的意思是:把字符串里面的大写字母转换为小写,这道题是想考怎么实现这个过程,过程也很简单,用ascii码,然后大写字母和小写字母相差32,如果知道这些就很好实现了。

代码

class Solution:
def toLowerCase(self, str: str) -> str:
t=''
for ch in str:
if(ch>='A' and ch<='Z'):
t+=chr(ord(ch)+32)
else:
t+=ch
return t

参考文献

​​[LeetCode] Python short 1 line ASCII & string method solutions​​


精彩评论(0)

0 0 举报