替换字符 replace
_string = 'hello world'
new_string = _string.replace('hello', 'world')
print(new_string)
删除字符串后三位
now=datetime.datetime.now().isoformat()
print(now,type(now))
now = now.replace(now[-3:],'')
print(now)
本文介绍Python中字符串替换和截取的实用方法,演示了如何使用replace()函数替换字符串中的特定子串,以及如何通过字符串切片删除指定位置的字符。
替换字符 replace
_string = 'hello world'
new_string = _string.replace('hello', 'world')
print(new_string)
删除字符串后三位
now=datetime.datetime.now().isoformat()
print(now,type(now))
now = now.replace(now[-3:],'')
print(now)
1077

被折叠的 条评论
为什么被折叠?