在 Python 中替换字符串的一部分? [英] Replace part of a string in Python?

查看:63
本文介绍了在 Python 中替换字符串的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用正则表达式从网页中获取一个字符串,该字符串的一部分可能包含我想用其他内容替换的内容.怎么可能做到这一点?我的代码是这样的,例如:

I used regular expressions to get a string from a web page and part of the string may contain something I would like to replace with something else. How would it be possible to do this? My code is this, for example:

stuff = "Big and small"
if stuff.find(" and ") == -1:
    # make stuff "Big/small"
else:
    stuff = stuff

推荐答案

>>> stuff = "Big and small"
>>> stuff.replace(" and ","/")
'Big/small'

这篇关于在 Python 中替换字符串的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆