How to remove all non-alphabetic characters in Python?
You can use regular expressions to remove all non-alphabetic characters. Here is an example code using regular expressions:
import re
def remove_non_letters(text):
return re.sub('[^a-zA-Z]', '', text)
text = "Hello, World! 123"
clean_text = remove_non_letters(text)
print(clean_text) # 输出:HelloWorld
In the above code, we defined a function called remove_non_letters that takes a string parameter, text. The re.sub function is used to replace all non-letter characters with an empty string. The regex expression [^a-zA-Z] represents matching any character that is not a letter. Finally, we print the text with non-letter characters removed.
Please note that only English letters are preserved in the above code. If you need to keep letters from other languages, you can modify the pattern of the regular expressions as needed.