0

I want to get 59,000 but I can't get what I want.

a='5M 9,000'
a.strip('M')

3
  • 1
    a = a.replace("M ", "")? Commented Oct 2, 2022 at 9:31
  • Does this answer your question? How to use string.replace() in python 3.x Commented Oct 2, 2022 at 9:32
  • Does the M stand for "million?" If so, then do you want 5,009,000 as the output? Commented Oct 2, 2022 at 9:34

3 Answers 3

1

You can use Regex module's sub() function, this returns a string where all matching occurrences of the specified pattern are replaced by the replace string.

Note:- '\D' is used to match all decimals, i think you're trying to extract numbers from the string?

 import re
 a = '5M 9,000'
 a=re.sub(r'\D', '', a)#59000
 print(f'{int(a):,}')

OUTPUT

59,000
Sign up to request clarification or add additional context in comments.

Comments

1
import re
a = '5M 9,000'
"".join(re.findall(r'\d+', a))

# 59000

or if want output with comma;

"".join([i for i in a if i.isnumeric() or i == ","])

# 59,000

Comments

0

Delete a single char at a specified location

a = '5M 9,000'
a = a[:c]+a[c+1:]

where c is the location of the char

For deleting multiple chars starting from a specified location

a = '5M 9,000'
a = a[:c]+a[c+length:]

where length is the number of chars you want to remove starting from the location c

a = '5M 9,000'
c = 3
a = a[:c]+a[c+1:]

a will be '5M ,000'

a = '5M 9,000'
c = 3
length = 2
a = a[:c]+a[c+length:]

a will be '5M 000'

making it into 59,000

a = '5M 9,000'
c = 1
length = 2
a = a[:c]+a[c+length:]

output a is '59,000'

1 Comment

oneline a = a[:1]+a[3:]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.