Go to System Settings and then click on Power. In the power setting, make sure that option for 'When the lid is closed' is set to Suspend. If you had a different setting here, you should check if you are able to suspend Ubuntu by closing the lid.
Here is what the above code is Doing:
1. We are using the subprocess module to run the command ‘gsettings get org.gnome.settings-daemon.plugins.power lid-close-suspend-with-external-monitor’
2. The output of the command is stored in the variable ‘output’
3. The output is then converted to a string using the decode() function
4. The output is then split into a list using the split() function
5. The second element of the list is then stored in the variable ‘result’
6. The result is then printed