本文摘自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实现logger打印功能
Python实现去除列表中重复元素的方法
Python怎么安装bokeh
Python如何实现无限循环
Python是编译型语言吗
Python如何删除列表一个元素
Python求解物理学中的双弹簧质能系统的代码实例
Python数据结构与算法之常见的分配排序法示例【桶排序与基数排序】_Python
解析Python实现mq消息队列以及消息队列的优点
Python简单地实现一键提取阴阳师原画方法
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 举例说明python中空格是属于字符