How to convert an integer to string in python - GMS - Learning Simply
Students' favourite free learning app with LIVE online classes, instant doubt resolution, unlimited practice for classes 6-12, personalized study app for Maths, Science, Social Studies, video e-learning, online tutorial, and more. Join Telegram

# How to convert an integer to string in python

Python works on different platforms like windows, mac, pi, etc. To work with the Python, We need to install the Python interpreter in our operating sy
Scroll Down and click on Go to Link for destination

In this Python Tutorial, We will learn how to convert an integer to string in python.

 How to convert an integer to string in python

Also, we will discuss:

• Python Code to convert an integer to string
• How to Convert a String to integer in python
• Convert a string to float in python
• How to convert bytes to string in Python
• Convert float to integer in Python
• How to convert an integer to float in Python
• Convert an integer to complex number in Python
• Python to convert string to datetime
• Python convert float to string
• Python convert int to binary
• Python convert int to octal
• Python convert int to hexadecimal
• Convert an object to string python
• Python convert string to hex
• Python convert string to a dictionary
• Python convert int to bytes
• Convert bytes to int in python
• Python convert letter to a number
• Check if a string is number python

Python works on different platforms like windows, mac, pi, etc. To work with the Python, We need to install the Python interpreter in our operating system. Here, I am using Python IDLE. But you can also use Pycharm, Spyder, etc.

If you are new to Python, check out the below tutorials:

## Convert an integer to string in python

Now, we will see how to convert an integer to string in Python.

To convert an integer to string in python, we need to follow these below steps:

Step-1

• To convert an integer to string, We have built-in function str() which converts the argument into a string format.
• The function can take any integer as an input and it can produce a string as an output.

Step-2

• First of all, We need to take an integer value. In the below python code, You can see, I have taken the value as “123”.
• Then we need to store the string value in “value_str”.
• Now print the “value_str” and it will convert the integer to string.

Below represents the Python code to convert an Integer to String.

value=123
value_str=str(value)
value_str

Step-3

After writing the above Python code (to convert an integer to string), Ones you will print the value_str, then the output will appear as a string value ‘123’. Also, you can refer to the below screenshot.

The above python code we can use to convert an integer to string in python.

## How to Convert a String to integer in python

In this, we will learn how to convert a string into integer in python.

Also, By taking a simple example, We will see how we can use the code to convert a string into integer in python and what will be the output.

### Python Code to convert a string to integer

To convert a string into integer in python, We need to follow these below steps:

Step-1

• To convert a string into integer. We have the built-in function int() method that takes a string as an argument and returns an integer value.
• The function can take any string as an input and it will return an integer as an output.

Step-2

• First of all, We need to take a string value. In the below code, You can see, I have taken the value as “111” as a string.
• Then we need to store the value in “value_int”.
• Now print the “value_int” and it will convert the string into integer.

Below represents the python code to convert a string to integer.

value='111'
value_int=int(value)
value_int

Step-3

After writing the above Python code (to convert the string into integer). Ones you will print the value_int then the output will appear as a integer value 111. Also you can refer to the below screenshot.

This is how to convert a String to integer in python.

## How to convert a string to float in python

In this scenarios, we will learn how to convert a string to float in python.

Also, By taking a simple example, We will see how we can use the code to convert a string to float in python and what will be the output.

### Python Code to convert a string to float

To convert a string to float in python, We need to follow these below steps:

Step-1

• To convert a string to float. We can use the built-in function float() method to convert a string to float and it will return a float value.
• The function can take any string as an input and it will return a float value as an output.

Step-2

• First of all, We need to take a string value. In the below code, You can see, I have taken the value as “12.5” as a string.
• Then we need to store the value in “value_f”.
• Now print the “value_f” and it will convert the string to float.

Below represents the python code to convert a string to float.

value = '12.5'
value_f = float(value)
value_f

Step-3

After writing the above python code(to convert the string to float). Once you will print the value_f then the output will appear as a float value 12.5. Also you can refer to the below screenshot.

## How to convert bytes to string in Python

In this section, we will learn how to convert bytes to string in python.

Also, By taking a simple example. We will see how we can use the code to convert a bytes to string in python and what will be the output.

### Python Code to convert bytes to string

To convert bytes to string in python, We need to follow these below steps:

Step-1

• To convert bytes to string in python, We have bytes class .decode() method which will convert the bytes and returns a string value.
• The function can take any bytes as an input and it will return a string as an output.

Step-2

• First of all, We need to take a bytes value. In the below code, You can see, I have taken the value as b”hello” as a bytes.
• Then we need to store the value in “value_b”.
• Now print the “value_b” and it will convert the bytes value to string.

Below represents the python code to convert bytes to string.

value = b'hello'
value_b = value.decode()
value_b

Step-3

After writing the above python code(to convert bytes to string). Once you will print the value_b then the output will appear as a string value ‘hello’. Also you can refer to the below screenshot.

The above Python code we can use to convert bytes to string in Python.

## How to Convert float to integer in Python

In this, we will learn how to convert a float to integer in python.

Also, By taking a simple example. We will see how we can use the code to convert float to integer in python and what will be the output.

### Python Code to convert float to integer

To convert float to integer in python, We need to follow these below steps:

Step-1

• To convert float to integer in python, We have the built-in function int() which will convert the positive or negative float value and returns integer with the round figure.
• The function can take any float as an input and it will return a integer as an output.

Step-2

• First of all, We need to take a float value. In the below code, You can see, I have taken the value as “15.2” as a float.
• Then we need to store the value in “value_i”.
• Now print the “value_i” and it will convert the float value to integer with round figure.

Below represents the python code to convert float to integer.

value = 15.2
value_i = int(value)
value_i

Step-3

After writing the above python code(to convert float to integer). Once you will print the value_i then the output will appear as a integer value “15” which is in round figure. Also you can refer to the below screenshot.

## How to convert integer to float in Python

In this, We will learn how to convert a integer to float in python.

Also, By taking a simple example. We will see how we can use the code to convert integer to float in python and what will be the output.

### Python Code to convert integer to float

To convert integer to float in python, We need to follow these below steps:

Step-1

• To convert integer to float in python, We have the built-in function float() which will convert the integer value and returns float value.
• The function can take any integer value as an input and it will return a float as an output.

Step-2

• First of all, We need to take integer value. In the below code, You can see, I have taken the value as “12” as a integer.
• Then we need to store the value in “value_f”.
• Now print the “value_f” and it will convert the integer value to float.

Below represents the python code to convert integer to float.

value = 12
value_f = float(value)
value_f

Step-3

After writing the above python code(to convert integer to float). Once you will print the value_f then the output will appear as a float value “12.0”. Also you can refer to the below screenshot.

## How to convert integer to complex number in Python

In this, We will learn how to convert integer to complex number in python.

Also, By taking a simple example. We will see how we can use the code to convert integer to complex number in python and what will be the output.

### Python Code to convert integer to complex number

To convert integer to complex number in python, We need to follow these below steps:

Step-1

• To convert integer to complex number in python, We can use complex() which will convert the integer to complex number as an argument or add imaginary part to the integer.
• The function can take any integer value as an input and it will return a complex number as an output.

Step-2

• First of all, We need to take integer value. In the below code, You can see, I have taken the value as “10” as a integer.
• Then we need to store the value in “complex_value”.
• Now print the “complex_value” and it will convert the integer value to complex number.

Below represents the python code to convert integer to complex number.

value = 10
complex_value = complex(value)
complex_value

Step-3

After writing the above python code(to convert integer to complex number). Once you will print the complex_value then the output will appear as a complex value “10+0j”. Also you can refer to the below screenshot.

This Python code we can use to convert integer to complex number in Python.

## Python to convert string to datetime

In this, We will learn how to convert string to datetime in python.

Also, By taking a simple example. We will see how we can use the code to convert string to datetime in python and what will be the output.

### Python Code to convert string to datetime

To convert string to datetime in python, We need to follow these below steps:

Step-1

• To convert string to datetime in python, We will use strptime() this function is available in datetime modules which will convert the string to datetime object.
• The function can take string value as an input and it will return datetime as an output.

Step-2

• First of all, We need to import datetime modules. After that we can take string value. In the below code, You can see, I have taken the value as “06/25/20 14:55:23”.
• Then we need to store the value in “datetime_o”. We have a format directives for datetime like “%m %d %y %H:%M%S” which match the format in the below example.
• Now print the “datetime_o” and it will convert the string value to datetime format.

Below represents the python code to convert string to datetime.

from datetime import datetime
datetime_s = '06/25/20 14:55:23'
datetime_o = datetime.strptime(datetime_s,'%m/%d/%y %H:%M:%S')
print(datetime_o)

Step-3

After writing the above python code(to convert string to datetime). Once you will print the datetime_o then the output will appear as “2020-06-25 14:55:23”.Also you can refer to the below screenshot.

Note: Year without century as a zero-padded decimal number we use “%y” for year.

## Python convert float to string

In python, to convert float to string we use built-in str() method for converting float numbers into the string.

Example:

my_float = 2.56
my_string = str(my_float)
print(my_string)
print(type(my_string))

After writing the above code ( python convert float to string ), Ones you will print ” my_string ” then the output will appear as a “ 2.56 ”.

Here, the str() function will convert the float value to a string and we can check whether the output is a string or not by printing type() which is a string. You can refer to the below screenshot python convert float to string.

This is how to convert float to string in Python.

## Python convert int to binary

In python, to convert integer to binary we will use built-in function bin() for converting the int number to binary.

Example:

int = 230
print(bin(int), "in binary.")

After writing the above code ( python convert int to binary ), Ones you will print ” bin(int) ” then the output will appear as a “ 0b11100110 in binary ”. Here, the bin() function will convert the int value to binary, and prefix “0b” is considered as binary. You can refer to the below screenshot python convert int to binary.

This is how to convert int to binary in Python.

## Python convert into octal

In python, to convert integer to octal we will use the built-in function oct() for converting the integer value to octal.

Example:

int = 230
print(oct(int), "in octal.")

After writing the above code ( python convert int to octal ), Ones you will print ” oct(int) ” then the output will appear as a “ 0o346 in octal ”. Here, the oct() function will convert the int value to octal, and the prefix “0o” is considered as octal. You can refer to the below screenshot python convert int to octal.

This is how to convert int to octal in Python.

## Python convert int to hexadecimal

In python, to convert integer to hexadecimal we will use the built-in function hex() for converting int to hexadecimal.

Example:

int = 230

After writing the above code ( python convert int to hexadecimal), Ones you will print ” hex(int) ” then the output will appear as a “ 0xe6 in hexadecimal ”. Here, the hex() function will convert the int value to hexadecimal, and the prefix “0x” is considered as hexadecimal. You can refer to the below screenshot python convert int to hexadecimal.

This is how to convert int to hexadecimal in Python.

## Convert an object to string python

In python, to convert object to string in python we can use the str() method for converting the object to string and the object will be in string form.

Example:

my_object = 12
string_object = str(my_object)
print(string_object)
print(type(string_object)

After writing the above code (convert an object to string python), Ones you will print ” string_object ” then the output will appear as a “ 12 ” which is converted to a string. Here, the str() function will convert the object to string and the type is ‘ str ‘. You can refer to the below screenshot convert an object to string python.

This is how to convert an object to string in Python.

## Python convert string to hex

In python, for converting a string to hexadecimal we will use hex() with an integer as a number and then it will be converted to hexadecimal.

Example:

my_string = "0xFF"
my_integer = int(my_string, 16)
my_value = hex(my_integer)
print(my_value)

After writing the above code (python convert string to hex), Ones you will print ” my_ value ” then the output will appear as a “ 0xff ”. Here, the hex() method converts the number to hexadecimal. You can refer to the below screenshot python convert string to hex.

The above code we can use to convert string to hex in Python.

## Python convert string to a dictionary

To convert string to a dictionary in Python, we will use ast.literal_eval for converting a string into the dictionary and we need to import ast.

Example:

import ast
my_string = '{"Anaisha": 101, "Tom": 202}'
my_dictionary = ast.literal_eval(my_string)
print(my_dictionary)

After writing the above code (python convert string to a dictionary), Ones you will print ” my_ dictionary ” then the output will appear as a “{‘Anaisha’: 101, ‘Tom’: 202}”. Here, to convert string to the dictionary we will call “ast.literal_eval”. You can refer to the below screenshot python convert string to a dictionary.

The above Python code we can use to convert string to a dictionary in Python.

## Python convert int to bytes

To convert an integer to bytes in Python, we will use integer.to_bytes(length, byteorder) on an integer with the desired length of the array as the length is the order of the array. The byteorder is set to “big”, the order of most significant bytes starts at the beginning of the array.

Example:

my_integer = 10
c = my_integer.to_bytes(2, 'big')
print(c)

After writing the above code (python convert int to bytes), Ones you will print ” c ” then the output will appear as a “ b’\x00\n’ ”. Here, the int.to_bytes() method converts the integer to bytes. You can refer to the below screenshot python convert int to bytes.

This is how to convert int to bytes in Python.

## Convert bytes to int in python

To convert bytes to int in python, we will use int.from_bytes() method and it will convert the bytes to an int value.

Example:

a_Bytes = b'\x00\n'
c = int.from_bytes(a_Bytes, byteorder='big')
print(c)

After writing the above code (convert bytes to int in python), Ones you will print ” c ” then the output will appear as a “ 10 ”. Here, the int.from_bytes() method converts the bytes to an integer value. You can refer to the below screenshot convert bytes to int in python.

This is how to convert bytes to int in Python.

## Python convert letters to a number

Let’s see how we can convert letters to a number in python.

To convert letters to numbers will result in a list, we will use ord() for converting letters to numbers in python.

Example:

letters = 'xyza'
num = []
for letter in letters:
number = ord(letter)-96
num.append(number)
print(num)

After writing the above code (python convert letters to a number), Ones you will print ” num ” then the output will appear as a “ [24, 25, 26, 1] ”. Here, for loop is used to iterate over each letter in a string. In each iteration, ord(letter)-96 with letter as a current letter to an initially empty list. You can refer to the below screenshot python convert letters to a number.

The above Python code, we can use to convert letters to a number.

## Check if a string is number python

In python, to check if all the characters are numeric we will use the method string.isnumeric() method which will return true if all the characters are number, otherwise false.

Example:

my_string = "101123"
a = my_string.isnumeric()
print(a)

After writing the above code (check if a string is number python), Ones you will print “a” then the output will appear as a “ True ”. Here, we will use isnumeric() method to check whether the string is number or not.

You can refer to the below screenshot python check if a string is number python

In this Python tutorial, We learned the conversion of several important data types to different data types with built-in methods in python. Like, convert an integer to string in python, how to Convert a String to integer in python, Python convert float to string, Python convert int to binary, Python convert int to octal, Python convert int to hexadecimal, Convert an object to string python, Python convert string to hex, Python convert string to a dictionary, Python convert int to bytes, Convert bytes to int in python, Python convert letters to a number.

Also, By taking a simple example, we saw how to use the code to convert data types to other data types with an output.