From Python 3.5 onward, the recommended way is to use subprocess.run. Since Python 3.7, to get the same behaviour as you describe, you would use:
cpe = subprocess.run("ls", shell=True, capture_output=True)
This will return a subprocess.CompletedProcess object. The output to stdout will be in cpe.stdout, the output to stderr will be in cpe.stderr, which will both be bytes objects. You can decode the output to get a str object by using cpe.stdout.decode() or get a by passing text=True to subprocess.run:
cpe = subprocess.run("ls", shell=True, capture_output=True, text=True)
In the latter case, cpe.stdout and cpe.stderr are both str objects.