Python Code to Convert Bytes to KB, MB, GB, & TB

Python Code to Convert Bytes to KB, MB, GB, & TB | In this article, we will discuss how to convert bytes into KB, MB, GB, and TB in Python. We will also see Python code to convert bytes to GB i.e. how to convert bytes to GB in Python. So, let’s start from the beginning. Firstly, we should know a little bit about Byte, KB, MB, GB, and TB.

Byte:- A byte is a unit of data memory equal to eight bits, depending on whether it requires error correction. One byte is equivalent to eight bits. A byte is a unit most computers use to describe a character such as a number, letter, or typographic symbol. 1 Byte = 8 bits.

KiloByte (KB):- A KiloByte is a unit of computer information or memory equivalent to 1024 bytes. 1 KB = 1024 bytes.

MegaByte (MB):- A MegaByte is a unit of computer information or memory equivalent to 1,048,576 bytes. 1 MB = 1024 KB = 1024 * 1024 bytes = 1,048,576 bytes.

GigaByte (GB):- A GigaByte is a unit of computer information or memory that is approximately equivalent to 1 billion bytes. 1 GB = 1024 MB = 1024 * 1024 * 1024 bytes = 1,07,37,41,824 bytes.

TeraByte (TB):- A TeraByte is a unit of computer information or memory that is approximately equivalent to about 1 trillion bytes. 1 TB = 1024 GB = 1024 * 1024 * 1024 * 1024 bytes = 1.099511628×10¹² bytes.

Now let us see how can we convert Bytes to KB, MB, GB, and TB in Python programming language. Below we have developed separate programs for these conversions and also for Python code to convert bytes to GB.

Python Code to Convert Bytes to KB

Here, we will develop a program to convert bytes into KiloBytes in Python programming language. One KiloByte (KB) is equal to 1024 Bytes therefore one byte is equivalent to 1/1024 KBs.

1 KB = 1024 Bytes
1 Byte = 1/1024 KB

Python Code to Convert Bytes to KB

byte = int(input("Enter Bytes: "))
kb = byte/1024
print("{} Kilo Bytes".format(kb))

Outputs for different input values:-

Enter Bytes: 1024
1.0 Kilo Bytes

Enter Bytes: 99999
97.6552734375 Kilo Bytes

Python Code to Convert Bytes to MB

Here, we will develop a program to convert bytes into MegaBytes in Python programming language. One MegaByte (MB) is equal to 1024 KiloBytes, so one MB is equivalent to 1024 * 1024 bytes.

1 MB = 1024 KB
1 MB = 1024 * 1024 Bytes = 10,48,576 Bytes
1 Byte = 1/10,48,576 MB

byte = int(input("Enter bytes: "))
mb = byte/(1024 * 1024)
print("{} MegaBytes".format(mb))

Outputs for different input values:-

Enter bytes: 9999
0.009535789489746094 MegaBytes

Enter bytes: 102400000
97.65625 MegaBytes

Enter bytes: 915415341315
873008.0998563766 MegaBytes

Python Code to Convert Bytes to GB

Here, we will discuss how to convert bytes to GB in Python programming language. One GigaByte (GB) is equal to 1024 MegaBytes (MB), so one GB is equivalent to 1024 * 1024 *1024 bytes.

1 GB = 1024 MB
1 GB = 1024 * 1024 * 1024 Bytes = 1,073,741,824 Bytes
1 Byte = 1/1,073,741,824 GB

How to Convert Bytes to GB in Python

byte = int(input("Enter bytes: "))
gb = byte/(1024 * 1024 * 1024)
print("{} Giga Bytes".format(gb))

Outputs for different input values for Python code to convert Bytes to GB:-

Enter bytes: 2684354560
2.5 Giga Bytes

Enter bytes: 536870912
0.5 Giga Bytes

Python Code to Convert Bytes to TB

Here, we will discuss how to convert bytes to TB in Python programming language. One TeraByte (TB) is equal to 1024 GigaBytes (GB), so one TB is equivalent to 1024 * 1024 *1024 * 1024 bytes.

1 TB = 1024 GB
1 TB = 1024 * 1024 * 1024 * 1024 Bytes = 1.099511628×10¹² bytes

byte = int(input("Enter bytes: "))
tb = byte/(1024 * 1024 * 1024 * 1024)
print("{} Tera Bytes".format(tb))

Outputs for different input values:-

Enter bytes: 2684354560123
2.441406250111868 Tera Bytes

Enter bytes: 536870912
0.00048828125 Tera Bytes

So, here we have learned how to convert Bytes to KB, MB, GB, and TB using Python Programming Language. We had also seen Python code to convert bytes to GB i.e. how to convert bytes to GB in Python. Hope you all find this article useful. Also see:- Convert Bytes to KB in Python, Python Convert Bytes to MegaBytes

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *