py 中 strip()移除头尾指定字符,replace()指定替换字符

strip() 方法

目的:  用于移除字符串头尾指定的字符(默认为空格)
用法:  str.strip([chars]) chars -->移除字符串头尾指定的字符。
返回值: 返回移除字符串头尾指定的字符 生成的新字符串。

例: str
= "www.cnblogs.com/qika" print(str.strip())       #当strip()的括号当中不传参数时,会默认为:去除首尾的空格 print(str.strip("w"))     #这样就会取消字符串的首尾所有的w 字符
replace() 方法

用法:str.replace(old,new[, max]) 目的: 字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。 参数解释: old
-- 要替换的字符串。 new -- 新的字符串,用于替换old字符串。 max -- 选择字符串 替换次数不超过 max 次 返回值:返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串, 如指定第三个参数max,则替换不超过 max 次。
例: str
= "www.cnblogs.com/qika" print ("我的博客地址:", str) print ("我的新的博客地址:", str.replace("qika", "xxx")) #指定要替换的内容qika,替换成为xxx. str = "this is my cnblogs address : www.cnblogs.com/qika; my cnblogs address is that !" print (str.replace("is", "was", 3)) #指定要替换的内容 is,替换为was,不超过3次
原文地址:https://www.cnblogs.com/QiKa/p/13532074.html