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

How to give comments in python?

0 Answers  


What is pytest in python?

0 Answers  


Tell me how does the ternary operator work in python?

0 Answers  


Can python be used for automation?

0 Answers  


Is self a keyword in python?

0 Answers  






How do you remove duplicates from a list in python whilst preserving order?

0 Answers  


How do I run Apache in Python?

0 Answers  


What is the most "pythonic" way to iterate over a list in chunks?

0 Answers  


What does mean in python?

0 Answers  


What does the __ init__ function do in python?

0 Answers  


Is google made with python?

0 Answers  


Does python have a-main() method?

0 Answers  


Categories