2. To Lower Case

Posted sxuer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2. To Lower Case相关的知识,希望对你有一定的参考价值。

Title:

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"

Note:

 None

Analysis of Title:

There is nothing to explain.

Test case:

"Hello"

Python:

class Solution(object):
  def toLowerCase(self, str):
  """
  :type str: str
  :rtype: str
  """
  res = ""
  for c in str:
    bse = ord(c)
    if bse<91 and bse>64:
      res+=chr(bse + 32)
    else:
      res+=c
  return res

Analysis of Code:

1.ord()  return the ASCII number

2.chr()  return the ASCII char

3. (64,91)  This is the Capital letters’s range, A = 65, Z = 90 

4. The capital letters add 32 equals Lowercase letters.

 

So, go through the str, if it‘s a capital, add 32, else it‘s a lowercase letters originally.

 

Notice: type(res)=str, and str+int=str, so "else:res+=c" is return str-type successful although c is int-type.

以上是关于2. To Lower Case的主要内容,如果未能解决你的问题,请参考以下文章

0709-To Lower Case(转换成小写字母)

0709-To Lower Case(转换成小写字母)

0709-To Lower Case(转换成小写字母)

709. To Lower Case

709. To Lower Case

709. To Lower Case