Python3 中的 isprintable()函数

首页 / Python3入门教程 / Python3 中的 isprintable()函数

如果字符串中的所有字符均可打印或字符串为空,则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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

黄勇的OKR实战笔记 -〔黄勇〕

Node.js开发实战 -〔杨浩〕

雷蓓蓓的项目管理实战课 -〔雷蓓蓓〕

JavaScript核心原理解析 -〔周爱民〕

后端技术面试 38 讲 -〔李智慧〕

现代C++编程实战 -〔吴咏炜〕

React Hooks 核心原理与实战 -〔王沛〕

后端工程师的高阶面经 -〔邓明〕

结构思考力 · 透过结构看思考 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)