How does Python convert a string with only numeric characters into a int using int() function

How does Python convert a string with only numeric characters into a int using int() function
python
Ethan Jackson

after looking into a lecture I have encountered a small problem. I don't understand the steps or how Python converts a numeric value inside a string into a int(or even a float) using there int() or float() function.

e.g #assign a variable

x = '3'

int(h)

**Please mind the fact that I have been trying this code in the python shell and experimenting. I have also tried Python tutor to show me how Python executes this code one by one, which has not been quite descriptive.

I just wish to know how python executes this code one by one and how it converts the string into a integer** Thank you for taking the time to read this :)

Answer

x = '3' # string to int x_int = int(x) print(x_int) # Output : 3 # string to float x_flt = float(x) print(x_flt) # Output : 3.0 # integer to float x_flt = float(x_int) print(x_flt) # Output : 3.0 # float to int x_int = int(x_flt) print(x_int) # Output : 3 # integer to string x_str = str(x_int) print(x_str) # Output : '3' (the quotes may not be present) # float to string x_str = str(x_flt) print(x_flt) # Output: '3.0' (the quotes may not be present)

Related Articles