2024-09-30 01:34:07 发布
网友
用空格()替换&的出现处,但第一个出现处除外
&
a = "abc&def&hij&klm" output = abc&def hij klm"
remove&多次出现,并在其中放置一个空格
a="abc&def&hij&klm" # split based on & character res = a.split('&', 1) # replace & with space in 2nd element res[1] = res[1].replace('&', ' ') # now join list into string print '&'.join(res)
使用字符串滑动:
ind = a.index('&') a = a[:ind+1] + a[ind+1:].replace('&', ' ')
简单方法:
output = '&'.join(s.replace('&', ' ') for s in a.split('&', 1))
使用字符串滑动:
简单方法:
相关问题 更多 >
编程相关推荐