首頁 >後端開發 >Python教學 >舉例說明python中空格是屬於字符

舉例說明python中空格是屬於字符

anonymity
anonymity原創
2019-06-13 14:26:578038瀏覽

python中空格屬於字元嗎?

答案是肯定的,空格在Python中也是屬於字元的。

舉例說明python中空格是屬於字符

案例:

#輸入一行字符,分別統計其中英文字母、空格、數字和其它字符的個數。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import string
s = raw_input('input a string:\n')
letters = 0
space = 0
digit = 0
others = 0
for c in s:
    if c.isalpha():
        letters += 1
    elif c.isspace():
        space += 1
    elif c.isdigit():
        digit += 1
    else:
        others += 1
print 'char = %d,space = %d,digit = %d,others = %d' % (letters,space,digit,othePython

判斷字元屬於數字、字母、空格的方法:

字元c.isalpha()

空格c.isspace()

數字c.isdigit()

以上是舉例說明python中空格是屬於字符的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn