How to convert an integer to string in python

By: On:

In this Python Tutorial, We will learn 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
  • Could not convert string to float python
  • Python convert string to hex
  • Python convert string to a dictionary

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.

How to convert integer to string in python
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.

convert 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 string to float in python
convert a string to float in python

How to convert bytes to string in Python

In this, 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.

How to convert bytes to string in python
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 float to integer in python

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 float in python

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.

How to convert integer to complex 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.

python to convert string to datetime

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.

Python convert float to string

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.

Python convert int to binary

Python convert int to 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.

Python convert int to octal

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
print(hex(int), "in hexadecimal.")

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.

Python convert int to hexadecimal

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.

Convert an object to string python

Could not convert string to float python

In python, to convert string to float in python we can use float() method. It can only convert the valid numerical value to a floating point value otherwise it will throw an error.

Example:

my_string = '1,400'
convert = float(my_string)
print(convert)

After writing the above code (could not convert string to float python), Ones you will print ” convert ” then the output will appear as a “ ValueError: could not convert string to float: ‘1,400’ ”. Here, we get the error because the value is not valid and we used a comma. You can refer to the below screenshot could not convert string to float python.

Could not convert string to float python

To solve this ValueError: could not convert string to float we need to give the valid numerical value to convert my string to float by using float() method.

Example:

my_string = '23.8'
convert = float(my_string)
print(convert)

After writing the above code (could not convert string to float python), Ones you will print ” convert ” then the output will appear as a “ 23.8 ”. Here, the float() method converts the string to float. You can refer to the below screenshot could not convert string to float python.

Could not convert string to float 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.

Python convert string to hex

Python convert string to a dictionary

In python, to convert string to a dictionary 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.

Python convert string to a dictionary

You may like the following Python tutorials:


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, Could not convert string to float python, Python convert string to hex, Python convert string to a dictionary.

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

Leave a Comment