Categories
Python Uncategorized

Remove non letter characters before and after characters in Python

To remove none letters characters from the beginning and end in python use regex from the re.py library. I needed it to remove spaces as well but not in between the words.

Here’s what I did:

import re

s = '( $My letters]'
s = re.sub('^[^a-zA-z]*|[^a-zA-Z]*$','',s)

print(s)

Output:

My letters