How do I convert a string to a number?



How do I convert a string to a number?..

Answer / 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

More Python Interview Questions

What is the difference between python and shell scripting?

0 Answers  


What is set () in python?

0 Answers  


How will you remove an object from a list?

0 Answers  


What are the two main data types in python?

0 Answers  


How do you protect python source code?

0 Answers  






What is subclass in python?

0 Answers  


Is javascript harder than python?

0 Answers  


Explain me what is the purpose of doc strings in python?

0 Answers  


Does python have a switch-case statement?

0 Answers  


How do you use raw_input in python?

0 Answers  


Can you run python programs without python installed?

0 Answers  


What are key words in python?

0 Answers  


Categories