Convert a number enclosed in parentheses (string) to a negative integer (or float) using Python?

前端 未结 7 2160
南方客
南方客 2021-02-19 21:52

In Python, what is the simplest way to convert a number enclosed in parentheses (string) to a negative integer (or float)?

For example, \'(4,301)\' to -4301, as commonly

7条回答
  •  醉梦人生
    2021-02-19 22:11

    The simplest way is:

    my_str = "(4,301)"
    num = -int(my_str.translate(None,"(),"))
    

提交回复
热议问题