「Python」2020.03.27学习笔记 | 第五章字符串-改写几个字符串函数

  • 学习测试开发的Day85,真棒!
  • 学习时间为1H20M
  • 第八次全天课(下午视频3H15M-4H03M_END)

改写lower()

代码:

def lower(s):
    if not isinstance(s,str):
        return s
    result=""
    for i in s:
        if (i>="A" and i<="Z"):
            result+=chr(ord(i)+32)
        else:
            result+=i
    return result
print(lower("我们124sdfRTYUI"))

输出:

PS D:\0grory\day8> python .\mylower.py
我们124sdfrtyui
PS D:\0grory\day8>

改写isletter()

算法:
判断所有的字符串,判断是否有一个不是字母,有一个不是就返回False,函数执行结束
代码:

def isletter(s):
    if not isinstance(s,str):
        return False
    for i in s:
        if not  ((i>="a" and i <="z") or (i>="A" and i<="Z")):
            return False
    return True
print(isletter("asdf"))
print(isletter("A士大夫"))
print(isletter(23423))
print(isletter("FGHsdf"))

输出:

PS D:\0grory\day8> python .\myletter.py
True
False
False
True
PS D:\0grory\day8>

判断是否是大写字母 is_upper_letter()

代码:

def is_upper_letter(s):
    if not isinstance(s,str):
        return False
    for i in s:
        if not  (i>="A" and i<="Z"):
            return False
    return True
print(is_upper_letter("ASDFG"))
print(is_upper_letter("asdf"))
print(is_upper_letter("A士大夫"))
print(is_upper_letter(23423))
print(is_upper_letter("FGHsdf"))

输出:

PS D:\0grory\day8> python .\my_upper_letter.py
True
False
False
False
False
PS D:\0grory\day8>

判断是否是小写字母 is_lower_letter()

代码:

def is_lower_letter(s):
    if not isinstance(s,str):
        return False
    for i in s:
        if not  (i>="a" and i<="z"):
            return False
    return True
print(is_lower_letter("ASDFG"))
print(is_lower_letter("asdf"))
print(is_lower_letter("A士大夫"))
print(is_lower_letter(23423))
print(is_lower_letter("FGHsdf"))

输出:

PS D:\0grory\day8> python .\my_lower_letter.py
False
True
False
False
False
PS D:\0grory\day8>

判断是否是字母+数字,改写is_alpnum()

代码:

def is_alpnum(s):
    if not isinstance(s,str):
        return False
    for i in s:
        if not  ((i>="a" and i <="z") or (i>="A" and i<="Z") or (i>='0' and i<='9')):
            return False
    return True
print(is_alpnum("asdf"))
print(is_alpnum("A士大夫"))
print(is_alpnum("23423"))
print(is_alpnum("FGHsdf"))

输出:

PS D:\0grory\day8> python .\my_alpnum.py
True
False
True
True
PS D:\0grory\day8>

改写find

版本1

代码:

def find(s,w):
    if not (isinstance(s,str) and isinstance(w,str)):
        return -1
    for i  in range(len(s)):
        if s[i:i+len(w)]==w:
            return i
print(find("sadf","d"))

输出:

PS D:\0grory\day8> python .\my_find.py
2
PS D:\0grory\day8>

版本2

代码:

def find(s,w,start=None,end=None):
    if not (isinstance(s,str) and isinstance(w,str)):
        return -1
    if start is None:
        start=0
    if end is None:
        end=len(s)
    if not (isinstance(start,int) and isinstance(end,int)):
        return -1
    for i  in range(len(s))[start:end]:
        if i+len(w)>=end+1:
            return -1
        if s[i:i+len(w)]==w:
            return i
    return -1
print(find("sadf","a"))
print(find("sadf","d",0,6))
print(find("sadf","df",0,4))
print(find("sadf","f",0,4))

输出:

PS D:\0grory\day8> python .\my_find.py
1
2
2
3
PS D:\0grory\day8>

>>> list(range(10)[3:1:-1])
[3, 2]
>>> list(range(10)[3:1])
[]
>>>

可以尝试下 replace、split

>>> a="abcdabcdabcd"
>>> a=list(a)
>>> a
['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']
>>> [1,1,1,1,c,d,1,1,1,1,c,d.....]

切片是可以超过范围的

>>> [1,2,3][1:8]
[2, 3]

>>> [1,2,3][11:18]
[]
>>>

你可能感兴趣的:(「Python」2020.03.27学习笔记 | 第五章字符串-改写几个字符串函数)