⑴ python里的幾個字元串處理函數
目前字元串直接加點的方法沒法增加,可行的方法是重載或者擴展類。下面是一個擴展的例子。class string(str): def __init__(self, s): self.s = s def totest(self): print('This is a test.for ' + self.s) if __name__ == '__main__': abc = string('abc') print(abc.split('b')) abc.totest()
⑵ python怎麼把字元串最後一個字元去掉
1、先將字元串轉換成列表,之後再修改列表中的元素來完成,通過list(r)來將r字元串轉化成了一個列表。
⑶ python字元串處理
s1=input('輸入字元串1:')
s2=input('輸入字元串2:')
s3=''.join([iforiins1ifinotins2])
print(s3)
⑷ python如何截取字元串到某個字元
答案:print a[0:6] /print a[:6]
以下為具體示例
str = 『0123456789』
print str[0:3] #截取第一位到第三位的字元
print str[:] #截取字元串的全部字元
print str[6:] #截取第七個字元到結尾
print str[:-3] #截取從頭開始到倒數第三個字元之前
print str[2] #截取第三個字元
print str[-1] #截取倒數第一個字元
print str[::-1] #創造一個與原字元串順序相反的字元串
print str[-3:-1] #截取倒數第三位與倒數第一位之前的字元
print str[-3:] #截取倒數第三位到結尾
⑸ python字元串運算符
可以使用eval()函數,表示執行字元串表示的代碼,例如你這個例子:
a='a'
b='in'
c='abc'
str="a"+b+"c"#拼接為"ainc"
printeval(str)#輸出True
⑹ Python字元串操作的split方法
str.split()沒有參數,代表以空字元分割,空字元包括空格、製表符、回車符、換行符等。因此,字元串中的空格和\n都是無參的split()的分割符。Line1-abcdef \nLine2-abc \nLine4-abcd分割後得到['Line1-abcdef', '', 'Line2-abc', '', 'Line4-abcd'],然後,split會拋棄得到的所有空字元串,因此最終結果就是['Line1-abcdef', 'Line2-abc', 'Line4-abcd']。
⑺ 常見的幾種python字元串方法總結
split([sep]) 將字元串分割為列表,默認用空白符分割,給出字元串參數,用參數字元串分割
'a b c'.split() 返回 ['a','b','c']
join 將可迭代對象中的字元串連接在一起
'\n'.join(['a','b','c'] )返回字元串 "a\nb\nc"
str.find(substr,[start,[end]]) 從str的下標 start至end之間查找substr,返回substr出現位置的下標,未找到返回-1
str.index 與find相仿,但未找到拋出異常
其餘還要通用的下標 ,切片操作等
⑻ python如何去除字元串中不想要的字元
問題:
過濾用戶輸入中前後多餘的空白字元
『 ++++abc123--- 『
過濾某windows下編輯文本中的』\r』:
『hello world \r\n』
去掉文本中unicode組合字元,音調
"Zhào Qián Sūn Lǐ Zhōu Wú Zhèng Wáng"
如何解決以上問題?
去掉兩端字元串: strip(), rstrip(),lstrip()
123456789101112131415
#!/usr/bin/python3 s = ' -----abc123++++ ' # 刪除兩邊空字元print(s.strip()) # 刪除左邊空字元print(s.rstrip()) # 刪除右邊空字元print(s.lstrip()) # 刪除兩邊 - + 和空字元print(s.strip().strip('-+'))
刪除單個固定位置字元: 切片 + 拼接
123456
#!/usr/bin/python3 s = 'abc:123'# 字元串拼接方式去除冒號new_s = s[:3] + s[4:]print(new_s)
刪除任意位置字元同時刪除多種不同字元:replace(), re.sub()
1234567891011
#!/usr/bin/python3 # 去除字元串中相同的字元s = '\tabc\t123\tisk'print(s.replace('\t', '')) import re# 去除\r\n\t字元s = '\r\nabc\t123\nxyz'print(re.sub('[\r\n\t]', '', s))
同時刪除多種不同字元:translate() py3中為str.maketrans()做映射
1234567
#!/usr/bin/python3 s = 'abc123xyz'# a _> x, b_> y, c_> z,字元映射加密print(str.maketrans('abcxyz', 'xyzabc'))# translate把其轉換成字元串print(s.translate(str.maketrans('abcxyz', 'xyzabc')))
去掉unicode字元中音調
#!/usr/bin/python3 import sysimport unicodedatas = "Zhào Qián Sūn Lǐ Zhōu Wú Zhèng Wáng"remap = { # ord返回ascii值 ord('\t'): '', ord('\f'): '', ord('\r'): None }# 去除\t, \f, \ra = s.translate(remap)'''通過使用dict.fromkeys() 方法構造一個字典,每個Unicode 和音符作為鍵,對於的值全部為None然後使用unicodedata.normalize() 將原始輸入標准化為分解形式字元sys.maxunicode : 給出最大Unicode代碼點的值的整數,即1114111(十六進制的0x10FFFF)。unicodedata.combining:將分配給字元chr的規范組合類作為整數返回。 如果未定義組合類,則返回0。'''cmb_chrs = dict.fromkeys(c for c in range(sys.maxunicode) if unicodedata.combining(chr(c))) #此部分建議拆分開來理解b = unicodedata.normalize('NFD', a)'''調用translate 函數刪除所有重音符'''print(b.translate(cmb_chrs))
⑼ python字元串處理問題
strip函數只能去除首尾字元,不能去掉中間字元
strip函數沒有副作用,也就是返回值才是去掉後的字元串
更簡潔的寫法left = ''.join([c for c in password if c in symbols])