Home  >  Article  >  Backend Development  >  How to remove numbers from string in python

How to remove numbers from string in python

WBOY
WBOYforward
2024-03-02 09:28:421420browse

How to remove numbers from string in python

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!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete