python学习笔记(五)—替换函数
replace()函数替换内容
用法:
replace(‘需要替换的内容’,‘替换后的内容’,替换次数)
备:如果需要替换单引号需要加’\’
例子:
str = "aaaaaa";
print str.replace("a", "b");
print str.replace("a", "b", 3);
输出结果:
bbbbbb
bbbaaa
strip()函数去除头尾指定内容
strip()去除头尾空格
例子:
str = ' a '
str = str.strip()
输出结果:
a
strip(‘需要去除的内容’),如果需要去除符号需要加’\’
例子:
str = '[=]a[=]'
str = str.strip('\[\=\]')
输出结果:
a