The Wayback Machine - https://web.archive.org/web/20230512160955/https://www.geeksforgeeks.org/string-to-int-and-int-to-string-in-python/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

String to Int and Int to String in Python

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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

Converting a string to an int

If we want to convert a number that is represented in the string to int, we have to use the int() function. This function is used to convert a number in given base to decimal.

Syntax: int(string, base)

Parameter :

string : consists of 1’s and 0’s

base : (integer value) base of the number.

Example:

Python3




# string data
n = '321'
print('Type of num is :', type(n))
  
# convert using int()
n = int(n)
print('So Now, type of num is :', type(n))

Output:

Type of num is : <class 'str'>
So Now, type of num is : <class 'int'>

Converting an Int to string

Converting an int datatype to string can be done with the use of str() function.

Syntax: str(object, encoding=’utf-8′, errors=’strict’)

Parameters:

object: The object whose string representation is to be returned.

encoding: Encoding of the given object.

errors: Response when decoding fails.

Example: 

Python3




hdv = 0x1eff
print('Type of hdv :', type(hdv))
  
# Converting to string
hdv = str(hdv)
print('Type of hdv now :', type(hdv))

Output:

Type of hdv : <class 'int'>
Type of hdv now : <class 'str'>
My Personal Notes arrow_drop_up
Last Updated : 28 Jul, 2020
Like Article
Save Article
Similar Reads
Related Tutorials