E-Learning

Learn Python – How To Parse String to Float or Int

 
To transform a string into Int or Float in Python, simply put, can be done using the following lines:

"57" to int:
Int ("57")
57

"23.578" to float:
Float ("23578")
23,578

These lines work perfectly when you know exactly the origin of your data. At times, however, you don’t know the data type of the string. We will complete the answer addressing some of these different cases.
 

Checking if the string value is an int or float in Python

If you use the conversion method for int in a string that was decimal values, it will send an error. Thus, we can use the code below to turn a string into int or float:

def a (s):
try:
return int (s)
except ValueError:
return  float (s)

Be very careful with this code because it will be mixing two types of numbers, which can cause problems in later operations using them.
 

To convert a float to int

If necessary subsequently conversion of a float created into an integer you can use this line:

Int (float (s))

Here we see what it does:

a = "273557"
Float (a)
273,557
int(float(a))
273

 

To round float in the transformation to int

The above method simply ignores the decimal values. To take them into account is necessary to use the following:

int (round (float (s)))

This shows to the system that it has to do a round operation. It will always round down. If you wanted a rounding up, do something like this:

int (round (float (s + 0.5)))

 

How to transform a string list

If you know exactly the type of data inside of the string, and it comes in a list format, you can make the transformation of all at once, using the code below:

x = ["527.1", "237.23", "876.54"]
Map (float x)
[527.1, 237.23, 876.54]

It also works with conversion to int.

If you know more ways how to make this conversion, share in the comments area below.

Now that you discovered the answer to this question and want to explore other, you can check our videos about learning Python. Below are some examples:

You can also subscribe to some channels that broadcast in Python, such as the following ones:

Zakko

jailbot

 
Another interesting way to find out cool things about Python is to access our project page!

 

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Blockchain in Elections: A Leap Toward Transparent Democracy

In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…

3 weeks ago

Win Big with Our Amazon Fire Max 11 & AirPods Pro Giveaway!

We’re thrilled to announce an exciting opportunity for you to win not one but two…

2 months ago

Unleashing Potential: How Education Ecosystem Transforms Learning into Real-World Success

Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…

4 months ago

The Role of Artificial Intelligence in Modern Software Development

Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…

7 months ago

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

8 months ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

9 months ago