[英]Python removing letters from a string?
Without using python's re.sub
, how would I remove and print ONLY
letters from a string with no spaces? 在不使用python的
re.sub
,如何从没有空格的字符串中删除并ONLY
打印字母?
For example I want ("ABCD!@#( EFGH@!(# IJ K912??")
to print out as ABCDEFGHIJK
例如,我想
("ABCD!@#( EFGH@!(# IJ K912??")
打印为ABCDEFGHIJK
Here's a simple approach: 这是一个简单的方法:
s = "ABCD!@#( EFGH@!(# IJ K912??"
print "".join(c for c in s if c.isalpha())
You could use the string class. 您可以使用字符串类。 Do you want upper case only, lower case, or both?
您只想使用大写字母还是小写字母? If upper, use string.ascii_uppercase, if lower, use string.ascii_lowercase, if both use string.ascii_letters.
如果较高,则使用string.ascii_uppercase;如果较低,则使用string.ascii_lowercase;如果两者均使用string.ascii_letters。 Example:
例:
import string
temp = "ABCD!@#( EFGH@!(# IJ K912??"
print(''.join([t for t in temp if t in string.ascii_letters]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.