阅读背景:

python 字符串单词大小分隔书写加空格

来源:互联网 
 
 
 
 
 
 
#将字符串如'weAREgoodSTUDENT'形式转换为'We are good student'
def switch(s):
    #分别匹配
    import re
    x=re.findall('[a-z]+',s)
    y=re.findall('[A-Z]+',s)
    #交替添加
    z=[];t=0
    while len(z)<=(len(x)+len(y)-1)*2:
        if t<len(x):
            z.append(x[t]);z=z+[' ']
        if t<len(y):
            z.append(y[t]);z=z+[' ']
        t+=1
        #去掉空格
        z= [str(i) for i in z]
        z1= ''.join(z)
        #转换小写,首字母大写
        z1=z1.lower().capitalize()
    return z1
s='weAREgoodSTUDENTxxxHHHweAREchildren'
print(switch(s))#将字符串如'weAREgoodSTUDENT'形式转换为'We are g



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: