You can convert a numeric string into an integer using int() method. The string must only contain numeric values without any additional characters or you will get a conversion error “ValueError: invalid literal for int() with base 10“.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## Convert string to int s = "0123456789" print(int(s)) ## Returns 123456789 print(type(int(s))) ## Returns s1 = "hello123" print(int(s1)) ## ValueError: invalid literal for ## int() with base 10: 'hello123' ## Convert int to string i = 987654321 print(str(i)) ## Returns 987654321 print(type(str(i))) ## Returns |