How the string does get converted to a number?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the key features of using python?

474


What is super () __ init__ in python?

492


How do I exit python?

447


Does python supports interfaces like in java or c#?

482


What is ord () in python?

417






How do I create a variable number of variables?

453


How do you debug a program in python? Is it possible to step through python code?

466


What is python magic method?

466


What does it mean if something is static?

440


Why do we need tuples?

431


Explain identity operators in python?

480


What is the use of __ init __ py in python?

452


What does == mean?

456


Why is using ‘eval’ a bad practice?

469


Where is python in anaconda?

429