4

My question is quite simple, I am trying to strip any character that is not A-Z, or 0-9 from a string.

Basically this is the process I am trying to do:

whitelist=['a',...'z', '0',...'9']

name = '_abcd!?123'

name.strip(whitelist)

print(name)

>>> abcd123

What's important to know is that I can't just only print valid characters in name. I need to actually use the variable in its changed state.

Alec
  • 8,529
  • 8
  • 37
  • 63
NeverEndingCycle
  • 173
  • 4
  • 13

3 Answers3

7

You can use re.sub and provide a pattern that exactly matches what you are trying to remove:

import re
result = re.sub('[^a-zA-Z0-9]', '', '_abcd!?123')

Output:

'abcd123'
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
3

Use string with a list comprehension

import string
whitelist = set(string.ascii_lowercase + string.digits)
name = ''.join(c for c in name if c in whitelist)
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
1

You can use simple regex:

new_string = re.sub('[chars to remove]', '', old_string)

Please also note that strings are immutable. You need to assign a new variable in order to change one.

Alec
  • 8,529
  • 8
  • 37
  • 63