python-字符串replace函数功能

# coding:utf-8

'''
replace的功能
    将字符串中的old(旧元素)替换成新的元素,并能指定替换的数量
replace的用法
    用法:
    newstr= string.replace(old,new,max)
    参数:
    old:被替换的元素
    new:替换old的新元素
    max:可选,代表替换几个,默认全部替换全部匹配的old元素

replace的注意事项
'''
# str1 = 'hello ,deweil'
# print(str1.replace('dewei','xiaomu'))
# print(str1.replace('l','0',1))  #替换1次
# print(str1.replace('l','0',2))  #替换2次
# print(str1.replace('l','0'))  #替换全部

str1 = "小慕买了一本书,58元,一个水杯20元"
str2 = "一共花了78元"

print(str1.replace('58元','¥58.00').replace('20元','¥20.00'))
print(str2.replace('78元','¥78.00'))

strcontent = 'hello imooc ha ha ha '
print(strcontent.strip().replace(' ',''))

版权声明:本文为weixin_37770279原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。