Playing with Strings and Colors in Python
thelivingpearl.com
thelivingpearl.com
colors = ('grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan')
for char in string:
print termcolor.colored(char, colors[string.index(char)%7]) for i,char in enumerate(string):
print termcolor.colored(char, colors[i%7])(3.2)
from itertools import cycle
for colour, character in zip(cycle(colours), string):
print(termcolor.colored(character, colour))
(2.7) from itertools import cycle, izip
for colour, character in izip(cycle(colours, string):
print termcolor.colored(character, colour)EDIT: The only issue is that if the string has a repeated letter it will be the same color.
from blessings import Terminal
t = Terminal()
print t.bold, 'Hi there!', t.normal
print t.bold_red_on_bright_green('It hurts my eyes!')
with t.location(0, t.height - 1):
print 'This is at the bottom.'
[1] http://pypi.python.org/pypi/blessings/ ATTRIBUTES = dict(
list(zip([
'bold',
'dark',
'',
'underline',
'blink',
'',
'reverse',
'concealed'
],
list(range(1, 9))
))
)
del ATTRIBUTES['']
It may feel like you are being more DRY by not typing the numerical values out. But this is data and it's perfectly fine to explicitly define them. The result is straightforward, more compact and easier to read as a result: ATTRIBUTES = {
'bold': 1,
'dark': 2,
'underline': 4,
'blink': 5,
'reverse': 7,
'concealed': 8
} attrs = ['bold', 'dark', '', 'underline', 'blink', '', 'reverse', 'concealed']
ATTRIBUTES = dict((k,v) for v,k in enumerate(attrs, 1) if k)
# or
ATTRIBUTES = dict(zip(itertools.count(1), attrs))"Python can be written as an algorithm that you can execute."