如果您考虑创建由破折号分隔的对,则可以使用以下函数:def pair_div(string): newString=str() #for storing the divided string for i,s in enumerate(string): if i%2!=0 and i<(len(string)-1): #we make sure the function divides every two chars but not the last character of string. newString+=s+'-' #If it is the second member of pair, add a dash after it else: newString+=s #If not, just add the character return(newString)例如:[In]:string="aazzxxcceewwqqbbvvaa"[Out]:'aa-zz-xx-cc-ee-ww-qq-bb-vv-aa'但是,如果您考虑将相同的字符分成一组并用破折号分隔,则最好使用正则表达式方法。