isspace_Python字符串isspace()

isspace

isspace_Python字符串isspace()_第1张图片

Python String isspace() function returns True if there are only whitespace characters in the string, otherwise it returns False. If the string is empty then isspace() function returns False.


如果字符串中仅包含空格字符,则Python String isspace()函数将返回True ,否则返回False 。 如果字符串为空,则isspace()函数将返回False

Python字符串isspace() (Python String isspace())

Some of the common whitespace characters are \t, \n, \r and obviously whitespace itself.

一些常见的空白字符是\ t,\ n,\ r,显然还有空白本身。

Let’s look at some examples of isspace() function.

让我们看一下isspace()函数的一些示例。

s = '   '
print(s.isspace())

s = '\t\n\r\t '
print(s.isspace())

s = '\u0009\t\u200a \u3000'
print(s.isspace())

Output:

输出:

True
True
True

打印所有空白字符Unicode数据 (Printing all whitespace characters Unicode Data)

We can use unicodedata module to print all the Unicode character codepoints that are treated as whitespace.

我们可以使用unicodedata模块来打印所有被视为空格的Unicode字符代码点。

import unicodedata

count = 0
for codepoint in range(2 ** 16):
    ch = chr(codepoint)
    if ch.isspace():
        print(u'{:04x}: ({})'.format(codepoint, unicodedata.name(ch, 'UNNAMED')))
        count = count + 1
print(f'Total Number of Space Unicode Characters = {count}')

Output:

输出:

0009: (UNNAMED)
000a: (UNNAMED)
000b: (UNNAMED)
000c: (UNNAMED)
000d: (UNNAMED)
001c: (UNNAMED)
001d: (UNNAMED)
001e: (UNNAMED)
001f: (UNNAMED)
0020: (SPACE)
0085: (UNNAMED)
00a0: (NO-BREAK SPACE)
1680: (OGHAM SPACE MARK)
2000: (EN QUAD)
2001: (EM QUAD)
2002: (EN SPACE)
2003: (EM SPACE)
2004: (THREE-PER-EM SPACE)
2005: (FOUR-PER-EM SPACE)
2006: (SIX-PER-EM SPACE)
2007: (FIGURE SPACE)
2008: (PUNCTUATION SPACE)
2009: (THIN SPACE)
200a: (HAIR SPACE)
2028: (LINE SEPARATOR)
2029: (PARAGRAPH SEPARATOR)
202f: (NARROW NO-BREAK SPACE)
205f: (MEDIUM MATHEMATICAL SPACE)
3000: (IDEOGRAPHIC SPACE)
Total Number of Space Unicode Characters = 29

Did you know there are so many whitespace characters? I certainly did not.

您知道空白字符太多吗? 我当然没有。

f-strings in Python. Python中的f字符串 。
GitHub Repository. GitHub存储库中签出更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/24255/python-string-isspace

isspace

你可能感兴趣的:(isspace_Python字符串isspace())