大家好,又见面了,我是你们的朋友全栈君。
使用字符串切片,如:
str1 = '1abcd.txt'
str2 = str1[1:-4]
print(str2)
Out : 'abcd'
使用replace()
,如:
删除:replace('指定字符','')
,是通过将指定字符替换为空实现的
str1 = 'abkcd'
str2 = str1.replace('k','')
print(str2)
Out : 'abcd'
替换
str1 = 'abkcd'
str2 = str1.replace('k','2')
print(str2)
Out : 'ab2cd'
``
注意,replace()
会删除、替换所有指定字符
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/136505.html原文链接:https://javaforall.cn