How the string does get converted to a number?



How the string does get converted to a number?..

Answer / chaitanya

• To convert the string into a number the built-in functions are used like int() constructor. It is a data type that is used like int (‘1’) ==1.

• float() is also used to show the number in the format as float(‘1’)=1.

• The number by default are interpreted as decimal and if it is represented by int(‘0x1’) then it gives an error as ValueError. In this the int(string,base) function takes the parameter to convert string to number in this the process will be like int(‘0x1’,16)==16. If the base parameter is defined as 0 then it is indicated by an octal and 0x indicates it as hexadecimal number.

• There is function eval() that can be used to convert string into number but it is a bit slower and present many security risks like __import__('os').system("rm -rf$HOME") - use of this will delete the home directory of the system.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Python Interview Questions

What is the namespace in python?

0 Answers  


Does apple use python?

0 Answers  


Which ide is best for python on windows?

0 Answers  


What are the reserved words in python?

0 Answers  


What are tuples used for?

0 Answers  






Which database is best with python?

0 Answers  


What are some common uses of Pickling in Python?

0 Answers  


what does yield function do? what is the difference between return and yield?

1 Answers  


What is difference between pass and continue in python?

0 Answers  


Are tuples mutable?

0 Answers  


What happens if an error occurs that is not handled in the except block?

0 Answers  


How to save an image when you know the url?

0 Answers  


Categories