To specify a Python replace number

Asked 2 years ago, Updated 2 years ago, 14 views

I want to delete the number using replace String (.replace('0', ").Is there a way to specify a number as a whole other than to enter all numbers from 0 to 9 in this way?

python

2022-09-21 20:22

2 Answers

munja = 'fda0sf1g3h54l6o8u7g9......'
for i in range(10):
    munja = munja.replace('%s'%i,'')
print munja
# Execution result: 'fdasfghloug...'

Or if you use a regular expression.

import re

munja = 'fda0sf1g3h54l6o8u7g9......'
num = re.compile('[\d]+')
print num.subn('', munja, 8)
# Execution result: ('fdasfghloug....', 8) #where 8 means 8 digits replaced by a space.


2022-09-21 20:22

You can change your mind and sort out the ones that aren't numbers.

In [1]: munja = 'fda0sf1g3h54l6o8u7g9'
In [2]: "".join(c for c in munja if not c.isdigit())                            
Out[2]: 'fdasfghloug'


2022-09-21 20:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.