Python:替换字符串中的术语(最后一个除外)

Python:替换字符串中的术语(最后一个除外),第1张

Python:替换字符串中的术语(最后一个除外)

在其中,

str.replace
您还可以传递一个可选的3rd参数
count
),该参数用于处理已完成的替换次数。

In [20]: strs = 'a;b;c;d'In [21]: count = strs.count(";") - 1In [22]: strs = strs.replace(';', ', ', count).replace(';', ' & ')In [24]: strsOut[24]: 'a, b, c & d'

帮助

str.replace

S.replace(old, new[, count]) -> stringReturn a copy of string S with all occurrences of substringold replaced by new.  If the optional argument count isgiven, only the first count occurrences are replaced.


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5035123.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存