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

How to remove numbers from string in python

WBOY
Release: 2024-03-02 09:28:42
forward
1442 people have browsed it

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)
Copy after login

The output result is:

abcdef
Copy after login

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!

Related labels:
source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template