You can use the zlib module available in python to compress and decompress the string. Refer to the code below:
import zlib
text="Hello world"
comp=zlib.compress(text)
print("Compressed: ", comp)
decomp=zlib.decompress(text)
print("Decompressed: ", decomp)
This should work.
To know more, It's recommended to join our Python Training in Chennai today.