Python: replace terms in a string except for the last

后端 未结 3 1852
北海茫月
北海茫月 2020-12-20 14:17

How does one go about replacing terms in a string - except for the last, which needs to be replaced to something different?

An example:

    letters =         


        
3条回答
  •  甜味超标
    2020-12-20 14:25

    letters = 'a;b;c;d'
    lettersOut = ' & '.join(letters.replace(';', ', ').rsplit(', ', 1))
    print(lettersOut)
    

提交回复
热议问题