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)
string : consists of 1’s and 0’s
base : (integer value) base of the number.
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’)
object: The object whose string representation is to be returned.
encoding: Encoding of the given object.
errors: Response when decoding fails.
Type of hdv : <class 'int'> Type of hdv now : <class 'str'>
- How to find the int value of a string in Python?
- Python bit functions on int (bit_length, to_bytes and from_bytes)
- Python | int() function
- Python program to convert any base to decimal by using int() method
- How to convert Float to Int in Python?
- Scala short ==(x: Int): Boolean
- What is the meaning of invalid literal for int() with base = ' '?
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- String slicing in Python to check if a string can become empty by recursive deletion
- String slicing in Python to rotate a string
- Python | Check if a given string is binary string or not
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if string ends with any string in given list
- Python | Sorting string using order defined by another string
- Python | Merge Tuple String List values to String
- Python | Sort each String in String list
- Python | Convert List of String List to String List
- Python - Length of shortest string in string list
- Python - Remove front K characters from each string in String List
- Python - Remove String from String List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.