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?
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?
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 ofpattern
instring
by the replacementrepl
.
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
.