20190112-自定义实现字符串的操作方法,如strip,upper,title,ljust,center,zfill,find,rfind等

时间:2023-11-25 17:50:14

1:自定义实现strip()
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
算法:strip()仅移除首尾的指定字符,不能移除中间的先从首部开始移除

def customerize_strip(s,value=' '):
result =''
front =0
end = len(s)
#step1:找到首部顺序开始一个非指定字符的index
for i in range(len(s)):
if s[i] == value:
continue
else:
front =i
break
#step2:找到尾部倒序开始第一个非指定字符的index
for j in range(len(s)-1,-1,-1):
#range是一个开区间,所有第一个值为len(s)-1,第二个值写作-1,表示取到第0位
if s[j]==value:
continue
else:
end = j
break
for k in range(front,end+1):
result+=s[k]
return result
print(s)
print(customerize_strip(s))

2:自定义实现str.upper()
算法,需要考虑字符串中不仅包含字母,可能还有其他字符的情况

def customerize_upper(s):
result=''
import string
for i in s:
if i in string.ascii_lowercase:
result+=chr(ord(i)-32)
else:
result+=i
return result
print(customerize_upper(s))

3.自定义实现str.lower()
算法,需要考虑字符串中不仅包含字母,可能还有其他字符的情况

def customerize_lower(s):
result=''
import string
for i in s:
if i in string.ascii_uppercase:
result+=chr(ord(i)+32)
else:
result+=i
return result
print(customerize_lower(s))

4.自定义实现str.swapcase()
Python swapcase() 方法用于对字符串的大小写字母进行转换

def customerize_swapcase(s):
result =''
import string
for i in s:
if i in string.ascii_uppercase:
result+=chr(ord(i)+32)
elif i in string.ascii_lowercase:
result+=chr(ord(i)-32)
else:
result+=i
return result
print(customerize_swapcase(s))

5.自定义实现str. capitalize()
Python capitalize()将字符串的第一个字母变成大写,其他字母变小写

def customerize_capitalize(s):
result =''
import string
for i in range(len(s)):
if i==0 and s[i] in string.ascii_lowercase:
result+=chr(ord(s[i])-32)
elif s[i] in string.ascii_uppercase:
result+=chr(ord(s[i])+32)
else:
result+=s[i]
return result
print(customerize_capitalize(s))

6.自定义实现str.title()
Python title() 方法返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写

def customerize_title(s):
result =[]
s= s.split()
import string
for i in s:
each =''
for j in range(len(i)):
if j==0 and i[j] in string.ascii_lowercase:
each += chr(ord(i[j])-32)
else:
each += i[j]
result.append(each)
return ' '.join(result)
s1= 'I am a good girl ha ha'
print(customerize_title(s1))

7.自定义实现str.ljust(numbe)
Python ljust() 方法返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串

def customerize_ljust(s,lenth,fillchar=' '):
if lenth<=len(s):
return s
else:
fill =''
for i in range(lenth-len(s)):
fill+=fillchar
result =s+fill
return result
print(customerize_ljust(s1,23,'*'))
print(len(s1))
print(len(customerize_ljust(s1,23,'*')))

8.自定义实现str.center(numbe)
center() 方法返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。如果指定的长度小于原字符串的长度则返回原字符串。

def customerize_center(s,lenth,fillchar=' '):
if len(s)>lenth:
return s
else:
result=''
front_period=int((lenth-len(s))/2)
end_period = lenth-front_period-len(s)
for i in range(lenth):
if i<front_period:
result+=fillchar
elif i>=front_period and i<lenth-end_period:
result+=s[i-front_period]
else:
result+=fillchar
return result
s2='aaa'
print(s2)
print(customerize_center(s2,7,'*'))

8.自定义实现zfill
Python zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0

def customerize_zfill(s,lenth):
if len(s)>lenth:
return s
else:
result=''
for i in range(lenth):
if i<lenth-len(s):
result+=str(0)
else:
result+=s[i-(lenth-len(s))]
return result
print(customerize_zfill(s2,10))

9.自定义find函数
Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1

def customerize_find(s,str,beg=None,end = None):
if beg == None:
beg = 0
if end ==None:
end = len(s)
for i in range(beg,end):
if s[i:i+len(str)]==str:
return i
else:
return -1
s3='lkadsacedsfhlkwheldsfsdfskadsface'
print(s3)
print(customerize_find(s3,'ace'))
print(customerize_find(s3,'ace',9))

10.自定义实现rfind
Python rfind() 返回字符串最后一次出现的位置(从右向左查询),如果没有匹配项则返回-1

def customerize_rfind(s,str,beg=None,end=None):
if beg == None:
beg = 0
if end ==None:
end = len(s)
for i in range(end,beg-1,-1):
if s[i-len(str):i]==str:
if i-len(str)>=beg:
return i-len(str)
else:
return -1
print(s3)
print(customerize_rfind(s3,'ace'))
print(customerize_rfind(s3,'ace',3,8))