Python练习题答案: 只有字母,请!【难度:1级】--景越Python编程实例训练营,1000道上机题等你来挑战

标签: python面试题库和答案  python编程练习  编程基础上  正则表达式  声明式编程

只有字母,请!【难度:1级】:

答案1:

import re

def remove_chars(s):
    return re.sub(r'[^a-zA-Z ]', '', s)

答案2:

import re

def remove_chars(s):
    return re.sub(r"[^a-zA-Z ]","",s)

答案3:

import re

def remove_chars(s):
    return re.sub(r"[^a-zA-Z ]", '', s)

答案4:

def remove_chars(s):
    return "".join( c for c in s if c.isalpha() or c==" " )

答案5:

def remove_chars(s):
    return ''.join(c for c in s if c.isalpha() or c==' ')

答案6:

def remove_chars(s):
    return "".join(c for c in s if c.isalpha() or c == ' ')

答案7:

import re
def remove_chars(s):
    return re.sub(r'[^A-Za-z\s]','',s)

答案8:

def remove_chars(s):
    return "".join(list(i for i in s if i.isalpha() or i.isspace()))

答案9:

def remove_chars(s):
    return ''.join(c for c in s if c.isalpha() or c.isspace())

答案10:

def remove_chars(s):
    #type your code here
    return "".join(c for c in s if c.isalpha() or c.isspace())

答案11:

import re

def remove_chars(s):
    return re.sub('(?i)[^a-z ]', '', s)

答案12:

def remove_chars(s):
    return ''.join(c for c in s if c.isspace() or c.isalpha())

答案13:

import re
remove_chars = lambda s: re.sub('[^a-z A-Z]','',s)

答案14:

import re
remove_chars = lambda s: re.sub('[^a-zA-Z\ ]', '', s)

答案15:

def remove_chars(s):
    w = ""
    for c in s:
        if c.isalpha() or c == " ":
            w += c
    return w​




Python基础训练营景越Python基础训练营QQ群

在这里插入图片描述
欢迎各位同学加群讨论,一起学习,共同成长!

原文链接:加载失败,请重新获取