如果字符串中的所有字符均可打印或字符串为空,则Python
isprintable()
它返回True或False。
如果该字符串是可打印的,则这是一个打印True的简单示例。请参见下面的示例。
链接:https://www.learnfk.comhttps://www.learnfk.com/python3/python-string-isprintable-method.html
来源:LearnFk无涯教程网
# 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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)