I want to remove all characters except a-z
, A-Z
, and spaces from a title. I then want to convert all the characters to lower case.
Asked
Active
Viewed 307 times
-6

Zach Gates
- 4,045
- 1
- 27
- 51

Bruce Cui
- 35
- 1
- 2
-
Welcome to Stack Overflow! Next time, please take a look at our ["How do I ask a good question?"](https://stackoverflow.com/help/how-to-ask) page. – Zach Gates Sep 01 '17 at 04:43
-
If regex is not mandotory, use the good `.lower` [doc](https://docs.python.org/3.4/library/stdtypes.html?highlight=str.lower#str.lower), related So question(https://stackoverflow.com/questions/6797984/how-to-convert-string-to-lowercase-in-python) – Drag and Drop Sep 01 '17 at 06:42
1 Answers
1
Use the built-in re
module.
re.sub(r'[^a-zA-Z ]', '', my_string).lower()
Here's an example:
>>> re.sub(r'[^a-zA-Z ]', '', 'This is 1 test string; I like it.').lower()
'this is test string i like it'

Zach Gates
- 4,045
- 1
- 27
- 51
-
should be `r'[^a-z A-Z]'` yours will remove the spaces as well which the OP wants to keep – officialaimm Sep 01 '17 at 04:45
-
1