Saturday, October 25, 2025
HomeLanguagesbz2.compress(s) in Python

bz2.compress(s) in Python

With the help of bz2.compress(s) method, we can get compress the bytes of string by using bz2.compress(s) method.

Syntax : bz2.compress(string)
Return : Return compressed string.

Example #1 :
In this example we can see that by using bz2.compress(s) method, we are able to compress the string in the byte format by using this method.




# import bz2 and compress
import bz2
  
s = b'This is GFG author, and final year student.'
print(len(s))
  
# using bz2.compress(s) method
t = bz2.compress(s)
print(len(t))


Output :

43
77

Example #2 :




# import bz2 and compress
import bz2
  
s = b'GeeksForGeeks@12345678'
print(len(s))
  
# using bz2.compress(s) method
t = bz2.compress(s)
print(len(t))


Output :

22
60

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS