題目:寫一個函式,它接受一個字串,做的事情和 strip()字串方法一樣,如果只
傳入了要去除的字串,沒有其他引數,那么就從該字串首尾去除空白字符,
否則,函式第二個引數指定的字符將從該字串中去除,
答案:
import re str1 = input('請輸入字串:') str2 = input('請輸入要洗掉的字符,若無請按回車:') def func(str1,str2): mo1 = re.compile(r'^\s*') mo2 = re.compile(r'\s*$') mo3 = re.compile(str2) re1 = mo1.sub('',str1) re2 = mo2.sub('',re1) re3 = mo3.sub('',re2) print(re3) func(str1,str2)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/150707.html
標籤:Python
