How do I convert a string to a number?

Answer Posted / chaitanya

Python contains several built-in functions to convert values from one data type to another data type.

The int function takes string and coverts it to an integer.

>>>s = "1234" # s is string

>>>i = int(s) # string converted to int

>>>print i+2

-------------------------

1236

The float function converts strings into float number.

>>>s = "1234.22" # s is string

>>>i = float(s) # string converted to float

>>>print i

-------------------------

1234.22

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is python dict a hashmap?

419


How do I run a py file in python shell?

447


What is the purpose of self?

472


Is notepad ++ good for python?

432


What is super () in python?

451






How is python interpreted language?

452


How do I delete a file?

502


What do you know about palindromes? Can you implement one in python?

499


How can I learn python?

439


How do I put python to sleep?

433


Tell me what are different ways to create an empty numpy array in python?

474


Why lambda forms in python does not have statements?

555


What does the meaning of slicing in python?

460


explain the reduce() function?

532


What are docstrings in python?

489