python

python print format函数的颜色与样式设置技巧有哪些

小樊
83
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
看了该问题的人还看了