How to print colour/color in python?

前端 未结 8 1148
有刺的猬
有刺的猬 2020-12-01 18:38

New to both Python and StackOverflow, I\'d like a little help. I\'d like to print color in Python and have Googled but with little luck :( I\'ve been confused each time and

8条回答
  •  伪装坚强ぢ
    2020-12-01 19:04

    If you're using a terminal and/or shell that support ANSI escape sequences, something like the following should work:

    print("Blah blah \033[0;32mthis part will be green\033[00m blah blah.")
    print("Blah blah \033[0;31mthis part will be red\033[00m blah blah.")
    

    I can confirm that it does work in bash on Linux. See the Wikipedia page on ANSI escape codes for further details, including a comprehensive table describing the effects of different character sequences/values. I don't advocate this as a canonical solution, but it may be sufficient for your purposes.

提交回复
热议问题