溫馨提示×

python print format函數的顏色與樣式設置技巧有哪些

小樊
98
2024-08-10 01:44:42
欄目: 編程語言

在Python中,我們可以使用ANSI轉義碼來設置打印輸出的顏色和樣式。下面是一些常用的技巧:

  1. 設置文字顏色:
print("\033[31mRed text\033[0m")  # 紅色文字
print("\033[32mGreen text\033[0m")  # 綠色文字
print("\033[33mYellow text\033[0m")  # 黃色文字
print("\033[34mBlue text\033[0m")  # 藍色文字
print("\033[35mMagenta text\033[0m")  # 品紅色文字
print("\033[36mCyan text\033[0m")  # 青色文字
print("\033[37mWhite text\033[0m")  # 白色文字
  1. 設置背景顏色:
print("\033[41mRed background\033[0m")  # 紅色背景
print("\033[42mGreen background\033[0m")  # 綠色背景
print("\033[43mYellow background\033[0m")  # 黃色背景
print("\033[44mBlue background\033[0m")  # 藍色背景
print("\033[45mMagenta background\033[0m")  # 品紅色背景
print("\033[46mCyan background\033[0m")  # 青色背景
print("\033[47mWhite background\033[0m")  # 白色背景
  1. 設置樣式:
print("\033[1mBold text\033[0m")  # 加粗文字
print("\033[4mUnderlined text\033[0m")  # 下劃線文字
print("\033[7mInverse text\033[0m")  # 反轉顏色

注意:在ANSI轉義碼中,\033 是轉義字符,[數字;數字m 是顏色和樣式控制碼,\033[0m 是恢復默認設置。

這些技巧可以幫助我們在Python中實現打印輸出的顏色和樣式設置。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女