Python 输出着色

150 阅读2分钟

在编写 Python 代码时,如果能够为输出结果添加颜色,可以使代码更加清晰易读,并便于用户理解。例如,可以将错误信息输出为红色,警告信息输出为黄色,成功信息输出为绿色。

2、解决方案

(1)使用 ANSI 转义序列

ANSI 转义序列是一种用于控制终端输出的特殊字符序列。它可以实现诸如改变文本颜色、设置背景颜色、移动光标等操作。

在 Python 中,可以使用 print() 函数输出字符串,并在字符串中添加 ANSI 转义序列。例如,以下代码将输出红色文本:

print("\x1b[31mThis is red text.\x1b[0m")

(2)使用第三方库

除了使用 ANSI 转义序列,还可以使用第三方库来为 Python 输出着色。例如,可以使用 colorama 库。

import colorama
from colorama import Fore, Back, Style

# 将输出的颜色设置为红色
colorama.init()
print(Fore.RED + "This is red text.")

# 将输出的颜色恢复为默认值
colorama.deinit()

(3)实战案例

下面是一个使用 Python 为密码强度测试结果着色的示例:

import re
import colorama
from colorama import Fore, Back, Style

# 初始化 colorama 库
colorama.init()

# 定义密码强度等级
strength = ['You didnt type anything','Terrible','weak sause','avarage','Good!','Very Strong', 'THE FORCE IS STRONG WITH THIS ONE']

# 获取用户输入的密码
password = input("Please type in the password you would like rated:")

# 初始化分数
score = 1

# 对密码进行检查并输出结果
if len(password) < 1:
    print(Fore.RED + strength[0] + Style.RESET_ALL)
elif len(password) >= 1 and len(password) <= 4:
    print(Fore.RED + strength[1] + Style.RESET_ALL)
else:
    print(Fore.GREEN + "Password was made stronger by not being short" + Style.RESET_ALL)

if len(password) >= 7:
    score += 1
    print(Fore.GREEN + "Password was made stronger by not being short" + Style.RESET_ALL)
else:
    print(Fore.RED + "Your password is really short, concider making it longer" + Style.RESET_ALL)

if len(password) >= 10:
    score += 1
    print(Fore.GREEN + "Password was made stronger by long" + Style.RESET_ALL)
else:
    print(Fore.RED + "An even longer password would make it stronger" + Style.RESET_ALL)

if re.search('[a-z]', password) and re.search('[A-Z]', password):
    score += 1
    print(Fore.GREEN + "Password was made stronger by having upper & lower case letters" + Style.RESET_ALL)
else:
    print(Fore.RED + "Indlucing both upper and lower case letters will make your password stronger" + Style.RESET_ALL)

if re.search('[0-9]+', password):
    score += 1
    print(Fore.GREEN + "Password was made stronger by using numbers" + Style.RESET_ALL)
else:
    print(Fore.RED + "Using numbers will make your password stronger" + Style.RESET_ALL)

if re.search('[.,!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]', password):
    score += 1
    print(Fore.GREEN + "Password was made stronger by using punctuation marks and characters" + Style.RESET_ALL)
else:
    print(Fore.RED + "Using punctuation marks and characters will make the password stronger" + Style.RESET_ALL)

# 输出最终密码强度评级
print("\nFinal password rating is:")
print(strength[score])

# 结束 colorama 库
colorama.deinit()

运行以上代码,可以得到如下输出:

Please type in the password you would like rated: 123456789aAbc

Password was made stronger by not being short
Password was made stronger by long
Password was made stronger by having upper & lower case letters
Password was made stronger by using numbers
Using punctuation marks and characters will make the password stronger

Final password rating is:
Very Strong