python中空格属于字符吗_举例说明python中空格是属于字符
生活随笔
收集整理的這篇文章主要介紹了
python中空格属于字符吗_举例说明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()
本文地址:http://itbyc.com/Python/21551.html
轉載請注明出處。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python中空格属于字符吗_举例说明python中空格是属于字符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iPhone 14系列蓄势待发:屏幕供应
- 下一篇: 计算几何 -- 旋转坐标系