11.实现字符串的find方法
def find(s,target_str):
if not isinstance(s,str):
return False
length=len(target_str)
for i in range(len(s)):
if s[i:i+length]==target_str:
return i
return -1
print(find('abc','ab'))
print(find('abc','c'))
12、实现字符串的isalpha方法
def isalpha(s):
if not isinstance(s,str):
return False
for v in s:
if not ((v >= "a" and v <= "z") or (v >= "A" and v <= "Z")):
return False
return True
print(isalpha("ABCd"))
print(isalpha("ABCd1"))
print(isalpha("adf"))
13、实现字符串的isdigit方法
def isdigit(s):
if not isinstance(s,str):
return False
for i in s:
if not i in '0123456789':
return False
else:
return True
print(isdigit('1234'))
print(isdigit('12acb12'))
14、实现字符串的isalnum方法
def isalnum(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 in '0123456789')):
return False
else:
return True
print(isalnum('abc1'))
print(isalnum('abc'))
print(isalnum('1343'))
print(isalnum('ad,fd'))
15、实现字符串的join方法
def join(s,join_str=None):
if not isinstance(s,str):
return False
if join_str==None:
join_str=''
result=''
for i in s:
result+=i+join_str
if join_str=='':
return result
return result[:-1]
print(join('abc','*'))
print(join('add',''))
python 练习2020/10/14
最新推荐文章于 2024-11-10 08:15:38 发布