For X11 (Unix / Linux):
os.system('echo "%s" | xsel -i' % variable)
xsel also gives you the ability to write:
If xsel does not work as you expect, probably because you are using the wrong choice / clipboard.
In addition, with the -a option, you can add to the clipboard instead of overwriting. Using -c clears the clipboard.
Improvement
The subprocess module provides a safer way to do the same:
from subprocess import Popen, PIPE Popen(('xsel', '-i'), stdin=PIPE).communicate(variable)
source share