Explore Courses Blog Tutorials Interview Questions
+2 votes
in Python by (3.4k points)
How can someone parse a numeric string like "121.5555" to it's corresponding float value 121.5555? or parse a string like "21" to an integer 21?

So can someone tell me how to parse a float str to a float and an integer str to an int.

1 Answer

0 votes
by (46k points)
edited by

There are several methods to perform this task but I personally recommend you to use ast.literal_eval It's the safe eval method to perform, check following code to your problem:

>>> import ast
>>> ast.literal_eval("121.555")
>>> ast.literal_eval("21")

Mention your doubts in comments. Cheers....!! 

To know more about this you can have a look at the following video tutorial:-

Learn more about Python from an expert. Enroll in our Python Course


Related questions

+2 votes
2 answers
0 votes
1 answer
asked Sep 25, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories