Sunday 27 December 2020

Python: Convert string to an integer

‘int(string)’ function takes a string as argument and convert it to an integer.

>>> data = "2345"
>>> 
>>> data
'2345'
>>> 
>>> int(data)
2345

 

You will get an error while converting non-numerical data.

>>> data = "hh"
>>> 
>>> int(data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'hh'

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment