Python Remove Comma In Dollar Amount

前端 未结 4 1454
失恋的感觉
失恋的感觉 2020-12-05 19:27

Using Python v2, I have the user entering an amount into a string as below:

RawPurchaseAmount = raw_input(\"Please enter purchase amount: \")

PurchaseAmount         


        
4条回答
  •  孤街浪徒
    2020-12-05 20:20

    Update StephenPaulger's answer is better.

    Original Answer

    If you don't mind regular expressions you can also replace both $ and , in one go. Something like this:

    >>> import re
    >>> RawPurchaseAmount
    '$10,000'
    >>> re.sub('[\$,]', '', RawPurchaseAmount)
    '10000'
    >>> 
    

    Remember to compile the regular expression if you plan to do this many times in the same program.

提交回复
热议问题