Convert String to Long in Python

Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing information about converting a string to long.

Converting String to long

A long is an integer type value that has unlimited length. By converting a string into long we are translating the value of string type to long type. In Python3 int is upgraded to long by default which means that all the integers are long in Python3. So we can use int() to convert a string to long in Python.

Syntax :

int(string, base)

Parameter :

string : consists of 1's and 0's
base : (integer value) base of the number.example 1

Example 1:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

a_string = "123"
print(type(a_string))
  
# Converting to long
a_long = int(a_string)
print(a_long)
print(type(a_long))

chevron_right


Output:

<class 'str'>
123
<class 'int'>

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

a='0x'
arr0 = '00000018000004000000000000000000'
arr1 = '00000000000000000000000000000000'
arr2 = 'fe000000000000000000000000000000'
arr3 = '00000000000000000000000000ffffff'
data = a+arr0+arr1+arr2+arr3
print(data)
print(type(data))
  
# Converting to long
data1 = int(data, 16)
print(type(data1))

chevron_right


Output:

0x0000001800000400000000000000000000000000000000000000000000000000fe00000000000000000000000000000000000000000000000000000000ffffff
<type ‘str’>
<type ‘int’>

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.