Python String isprintable() 方法
isprintable() 方法如果字符串中的所有字符都是可打印的,则返回 True。如果不是,则返回 False。
示例
text = 'apple'
# 如果 text 是可打印的,则返回 True
result = text.isprintable()
print(result)
# 输出: True
isprintable() 语法
isprintable() 方法的语法是:
string.isprintable()
这里,isprintable() 检查 string 是否可打印。
注意:
-
占据屏幕打印空间的字符被称为 可打印字符。例如字母和符号、数字、标点符号、空白字符。
-
不占据空间且用于格式化的字符被称为 不可打印字符。例如换行符、分页符。
isprintable() 参数
isprintable() 方法不接受任何参数。
isprintable() 返回值
isprintable() 方法返回:
True- 如果字符串中的所有字符都是可打印的False- 如果字符串包含至少一个不可打印字符
示例 1:Python String isprintable()
text1 = 'python programming'
# 检查 text1 是否可打印
result1 = text1.isprintable()
print(result1)
text2 = 'python programming\n'
# 检查 text2 是否可打印
result2 = text2.isprintable()
print(result2)