string to numeric conversion

Posted on 2005-05-10
Last Modified: 2010-04-16
I have a string "70%"

but i need to convert it to decimal, so I can compare it against different level?

How can thos conversion be perfomed?
Question by:RobertoDeAlba
    LVL 14

    Accepted Solution

    First get just the numeric part of the string:

    STR = "70%"
    import re
    match = re.match("([0-9]+)", STR)

    then convert the numeric part of the string to an integer:

    value = int(

    'value' is now 70 (an integer).

    If you can have fractions (eg. "70.5%") then you need to match the decimal point as well as the digits, and convert to a float:

    STR = "70.5%"
    import re
    match = re.match("([0-9.]+)", STR)
    value = float(

    'value' is now 70.5 (a float).

    Author Comment

    Thanks, thatis exactly what I needed

    Best Regards

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now