# -*- coding: utf-8 -*-
# @__author__:choppa
# @DATA 2021/8/30
# 将DeleteUser,处理成deleter_user
a = 'DeleteUser'
print(a.index('D'))
positionlist = []
for i in a:
if i.isupper() == True:
positionlist.append(a.index(i))
endlist = []
a = a.lower()
print(positionlist)
for i in positionlist:
if i > 0:
endlist.append(a[0:i])
endlist.append(a[i:])
ans = "_".join(endlist)
print(ans)
输出结果:
0
[0, 6]
delete_user
封装函数:
# -*- coding: utf-8 -*-
# @__author__:choppa
# @DATA 2021/8/30
# 将DeleteUser,处理成deleter_user
def word_handler(starword):
positionlist = []
for i in starword:
if i.isupper() == True:
positionlist.append(starword.index(i))
endlist = []
starword = starword.lower()
for i in positionlist:
if i > 0:
endlist.append(starword[0:i])
endlist.append(starword[i:])
ans = "_".join(endlist)
return ans
if __name__ == '__main__':
anns = word_handler("DeleteUser")
print(anns)