字符串替换方法replace
str1 .replace(old_str,new_str,count)字符串的替换,将str1中的 old_str 替换成new_str
old_str:将要被替换的字符串
new_str:新的字符串,替换成的字符串# count:替换的次数,默认是全部替换
返回值:得到一个新的字符串,不会改变原来的字符串
str1 = 'hello world IT Python and Pythonxcc'
str2 = str1.replace( 'Python', 'java')
print( 'my_str:', str1)
print( 'my_str1:', str2)
str3 = str1.replace( 'Python', 'java',1) # 替换一次
print( ' my_str2: ', str3)

本文介绍了Python中的字符串替换方法`replace()`,通过示例展示了如何使用该方法将字符串中的特定内容替换为其他文本。示例中,将字符串`'helloworldITPythonandPythonxcc'`中的`'Python'`替换为`'java'`,并演示了替换一次和全部替换的区别。
463

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



