Python字符串的替换

请将字符串str1=“This is my VB program!”中的“Vb”替换成“Python”,输出替换前后的结果。

str1="This is my VB program!"
str = str1.replace("VB", "Python")
print(str)

a='This is my VB program!'
a.replace('VB ' , 'Python')

方法一:

>>> str1="This is my VB program!"
>>> str1 = str1.replace('VB', 'Python')
>>> str1
'This is my Python program!'

方法二:

>>> str1 = "This is my VB program!"
>>> lst = str1.split()
>>> lst[3] = 'Python'
>>> str1 = ' '.join(lst)
>>> str1
'This is my Python program!'
请看👉 :Python字符串的判断
你还可以看下python参考手册中的 python- 字符串