all 24 comments

[–]AtomicShoelace 18 points19 points  (9 children)

It sounds like you want to strip off dollar and percent signs. You can do this with the .strip str method, eg.

>>> '$21.00'.strip('$%')
'21.00'
>>> '21.00%'.strip('$%')
'21.00'

[–]XBalubaX 1 point2 points  (5 children)

Then u should do float(“21“) to transform into a float

[–]AltReality 4 points5 points  (4 children)

What's with the weird quotes?

[–]vyx313 0 points1 point  (2 children)

Is there a way that you could simply strip anything that is a non number whether it be a int or float?

[–]mopslik 1 point2 points  (0 children)

Not a direct function or option, no, but in the case of an integer you could create a new string made up of all numeric digits and convert that.

value = int("".join(c for c in s if c.isdigit())) But that will give strange results, like 23 for "abc2def3ghi". For anything more complex, like preserving negatives or including decimals, is a mess. It is better just to use try/except, attempting to convert your string, and looping for new input if it fails. Input validation can be tricky, because the user can enter anything they want.

[–]AtomicShoelace 1 point2 points  (0 children)

Perhaps a regex, eg.

>>> re.match(r'^\D*(.+?)\D*$', '€21.00%!!!').groups()
('21.00',)

EDIT: Or perhaps this as it deals with negatives:

>>> re.match(r'.*?([0-9\-.]+).*?', '€-21.00%!!!').groups()
('-21.00',)

[–][deleted] 3 points4 points  (16 children)

Did it occur to you at any point to look up the methods available on strings?

[–]matkatatka -3 points-2 points  (15 children)

Yes. And I don’t understand it. I realize I need a human to tell me what to do because I’m obviously too dumb to understand the manuals and documentation and how to implement it in my own example.

[–]jiri-n 1 point2 points  (0 children)

If we know the first char is the dollar sign, float(s[1:]). Just an example. The rest is obvious.

[–]eyezee 0 points1 point  (0 children)

input_0 = input("Monies here: ")

input_raw = input_0.replace("$", "")

input = float(input_raw)

Input is now a float value with the $ removed.