-2

I want that you can give a string (Userinput) and then I want to delete all characters that aren't letters (so a-z and A-Z are the only ones which stay in the string).

Can anyone help me?

BOB
  • 47
  • 7

1 Answers1

2

You need to use Python's re (regular expression) module. More specifically you could try using the re.sub method. Here is a quote from the official documentation:

re.sub(pattern, repl, string, count=0, flags=0) Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.

For example, if your string is 'God damn 47', you could do re.sub('[0-9]', '', 'God damn 47') and get 'God damn '. This will substitute any digit within the string with '' (empty string) which is equivalent to deleting them. Remember to import the re module with the statement import re.

Mike
  • 484
  • 4
  • 13