一、去除字符串中的数字空格
test_str = "02101 Hello Python 10310"
#filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。
#函数isalpha ()的作用是检查一个字符串中所有的字符是否都是由字母构成的,并且至少有1个字符
result_str1 = ''.join(filter(str.isalpha,test_str))
result_str =''.join(filter(str.isalnum,test_str))
result_str2 = test_str.replace(' ','')
result_str3 = test_str.strip('0123')
print('原字符串str:' + test_str)
print('新字符串str1:'+ result_str1)
print('新字符串str2:'+ result_str2)
print('新字符串str:'+ test_str.strip('0123'))
print('新字符串str3:'+ result_str3.replace(' ',''))
print('字符串str:' + result_str)
二、现有一段英文,将 I 误写为 i ,请纠正
a_str = " i i i love you oh~ babe"
a_str1 = a_str.replace('i','I')
print("改正后" + a_str1)