网络编程
位置:首页>> 网络编程>> Python编程>> python清除字符串里非字母字符的方法

python清除字符串里非字母字符的方法

作者:pythoner  发布时间:2022-08-27 16:04:02 

标签:python,清除,字符串

本文实例讲述了python清除字符串里非字母字符的方法。分享给大家供大家参考。具体如下:


s = "hello world! how are you? 0"
# Short version
print filter(lambda c: c.isalpha(), s)
# Faster version for long ASCII strings:
id_tab = "".join(map(chr, xrange(256)))
tostrip = "".join(c for c in id_tab if c.isalpha())
print s.translate(id_tab, tostrip)
# Using regular expressions
print re.sub("[^A-Za-z]", "", s)

希望本文所述对大家的Python程序设计有所帮助。

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com