python习题库字符串1-11

习题1:一个句子中的所有数字和标点符号删除

方法1:

#encoding=utf-8
s="i am a boy, my age is 19 years."
import string
result=""
for letter in s:
    if not letter.isdigit()  and letter not in string.punctuation:
        result+=letter
print(result)

在这里插入图片描述
方法2:

#encoding=utf-8
import string
s="i am a boy, my age is 19 years."
result=""
for letter in s:
    if letter in string.ascii_letters or letter==" ":
        result+=letter
print(result)

在这里插入图片描述
方法3:

#encoding=utf-8
import string
s="i am a boy, my age is 19 years."
for letter in s:
    if letter in "0123456789":
        s=s.replace(letter,"")
    if letter in string.punctuation:
        s=s.replace(letter,"")
print(s)

在这里插入图</div></body></html>

你可能感兴趣的:(python习题库)