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 class variable in python?

0 Answers  


What is the difference between while and for loop?

0 Answers  


Explain relational operators in python?

0 Answers  


Give an example to capital first letter of a string?

0 Answers  


Do you know how python is interpreted?

0 Answers  






What does tuple mean?

0 Answers  


How would you declare a comment in python?

0 Answers  


Is set nesting implemented in python?

0 Answers  


Use of double quotes and single quote in python?

0 Answers  


What is special about python?

0 Answers  


How is numpy different from scipy?

0 Answers  


How do you sort in python?

0 Answers  


Categories