Changing BASH script to Python
Question: I am trying to convert this script to Python but am having problems with the
BASH syntax `${variable}`. This does something I do not understand as I have
very little experience with BASH scripting. My question is basically,how can I
do the dollar brackets variable thing in Python `${variable}`. In my case the
variable is a number from zero to two-hundred and fifty-five. Please see the
BASH script below. I am trying to do the exact same thing in Python.

    
    
    do     
    
        for code in {0..255};        
            do echo -e "\e[38;05;${code}m $code: Test";        
            done; 
    
    done
    

Here is my attempt to convert the code to a Python script. Unfortunately, I am
still having to call BASH via the os.system() method or function. Please check
out my script below. The script does not function the same way though, with
changing the text in the BASH shell. The Python script below simply prints out
the crazy text below and increments the numbers... :/

    
    
    #! /usr/bin/env python
    
    import os
    
    def run():
        for code in range(0, 255):
            os.system('echo -e "\e[38;05;%dm %s Test"' % (code,str(code)))
    
    
    
    run()
    


Answer: You can use `print` command and [string
formatting](https://docs.python.org/2/library/string.html#string-formatting)
to evaluate your variable during the print.

BTW, you can use `xrange` rather than `range` in order not to generate all the
numbers in your memory, but to `yield` it one-by-one (for large range)

You can use this:

    
    
    import os
    def run():
       for code in range(0, 256):
         print "\x1b[38;05;{code}m {code} Test".format(code=code)
    
    run()
    

