Adding to falsetru's answer, Python (2 and 3) also supports writing regular strings to stdout:
import sys
sys.stdout.write('\33]0;title you want\a')
sys.stdout.flush()
By the way, I put this in my ~/.pythonstartup
file to set the title when opening a Python shell:
import os
import sys
def set_xterm_title(title='Python %d.%d.%d' % sys.version_info[:3]):
'''
Set XTerm title using escape sequences.
By default, sets as 'Python' and the version number.
'''
sys.stdout.write('\33]0;' + title + '\a')
sys.stdout.flush()
# Make sure this terminal supports the OSC code (\33]),
# though not necessarily that it supports setting the title.
# If this check causes compatibility issues, you can add
# items to the tuple, or remove the check entirely.
if os.environ.get('TERM') in (
'xterm',
'xterm-color',
'xterm-256color',
'linux',
'screen',
'screen-256color',
'screen-bce',
):
set_xterm_title()