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 pythonpath variable?

0 Answers  


What is meant by mutex in python?

0 Answers  


Why is numpy so fast?

0 Answers  


How to print without newline or space?

0 Answers  


What is class super?

0 Answers  


What does != Mean in python 3?

0 Answers  


How do you append to a list in python?

0 Answers  


What is python's slice notation?

0 Answers  


What is enumerate python?

0 Answers  


Why are tuples immutable?

0 Answers  


Explain the uses of the modules sqlite3, ctypes, pickle, traceback, and itertools.

0 Answers  


What does do in python?

0 Answers  


Categories