import shlex tests = [ "foo#bar", "foo #bar" ] for t in tests: print "%s -> %s" % (t,[x for x in shlex.shlex(t,posix=True)])