无涯教程-Python3 - isprintable()函数

75 阅读1分钟

如果字符串中的所有字符均可打印或字符串为空,则Python isprintable()方法返回True。如果字符串中的任何字符为"不可打印",则返回False。

isprintable - 语法

isprintable()

isprintable - 返回

它返回True或False。

如果该字符串是可打印的,则这是一个打印True的简单示例。请参见下面的示例。

# Python isprintable() method example
# 变量声明
str = "Hello, Learnfk"
# 函数调用
str2 = str.isprintable()
# 显示结果
print(str2)

输出

True

在这里,无涯教程在字符串中使用其他字符,例如换行符和制表符。请参见示例的结果。

# Python isprintable() method example
# 变量声明
str = "Hello, Learnfk"
str2 = "Learn Java here\n"
str3 = "\t Python is a programming language"
# 函数调用
str4 = str.isprintable()
str5 = str2.isprintable()
str6 = str3.isprintable()
# 显示结果
print(str4)
print(str5)
print(str6)

输出

True
False
False

测试也包含特殊字符的不同字符串值。如果是特殊字符,则方法返回False。

# Python isprintable() method example
# 变量声明
str = "Hello, Learnfk"
if str.isprintable() == True:
    print("It is printable")
else:
    print("Not printable")
str = "$Hello@Learnfk#"    # Special Chars
if str.isprintable() == True:
    print("It is printable")
else:
    print("Not printable")
str = "Hello\nLearnfk"
if str.isprintable() == True:
    print("It is printable")
else:
    print("Not printable")

输出

It is printable
It is printable
Not printable

参考链接

www.learnfk.com/python3/pyt…