You can use regular expressions to remove numbers from strings. Examples are as follows:
import re def remove_numbers(string): pattern = r'\d+' return re.sub(pattern, '', string) string = 'abc123Def456' result = remove_numbers(string) print(result)
The output result is:
abcdef
In the above example, we use the re.sub()
function to replace the matched number with an empty string and return the result. Regular expression r'\d '
is used to match one or more consecutive numbers.
The above is the detailed content of How to remove numbers from string in python. For more information, please follow other related articles on the PHP Chinese website!