本文摘自php中文网,作者anonymity,侵删。
python中空格属于字符吗?答案是肯定的,空格在Python中也是属于字符的。

案例:
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #!/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中空格是属于字符的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
用 Python 连接 mysql 的几种方式详解_Python
如何访问Python字典里的值?(实例解析)
Python 的二元算术运算详解
Python怎么卸载模块
Python如何判断是不是回文数
Python 通配符删除文件
Python有split函数吗
Python多线程爬虫实战_爬取糗事百科段子的实例_Python
学习Python的几个不同阶段
Python中关于正则捕获操作的示例
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 举例说明python中空格是属于字符