random color text generat
Question: I am trying to make a program in python that will accept an argument of text
input, then randomly change each letter to be a different color

This is what I have:

    
    
    color = ['red' , 'blue', 'green' , 'purple' , 'yellow' , 'pink' , '#f60' , 'black' , 'white'];
    

I want to be able to have a program that can let me type out a paragraph, then
it uses the colors in the list and randomly assigns them to each letter.

For an output, I would like to to be something like this.

    
    
    [color=random]H[/color][color=random]i[/color] [color=random]t[/color] [color=random]h[/color] [color=random]e[/color][color=random]r[/color][color=random]e[/color]
    

The colors should be random from the list I made. Is this possible to make?


Answer: This works for me:

    
    
    from random import choice
    
    color = ['red' , 'blue', 'green' , 'purple' , 'yellow' , 'pink' , '#f60' , 'black' , 'white']
    l = len(color)
    
    str = "Hit Here"
    
    html = ''
    for x in str:
        html += '[color=' + choice(color) + ']' + x + '[/color]' if len(x.strip()) > 0 else x
    
    print(html)
    

Sample output:

    
    
    [color=yellow]H[/color][color=#f60]i[/color][color=yellow]t[/color] [color=black]H[/color][color=black]e[/color][color=white]r[/color][color=purple]e[/color]
    
    [color=purple]H[/color][color=white]i[/color][color=purple]t[/color] [color=black]H[/color][color=green]e[/color][color=red]r[/color][color=blue]e[/color]
    
    [color=yellow]H[/color][color=green]i[/color][color=#f60]t[/color] [color=blue]H[/color][color=white]e[/color][color=green]r[/color][color=pink]e[/color]
    

