本文摘自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中线程终止与挂起的实现方法
Python中关于str与repr的使用详解
Python中pow什么意思
人工智能为什么用Python
Python中使用三种方法判断文件或文件夹是否存在的实例分享
Python中knn算法(k-近邻算法)的详细介绍(附示例)
Python中的并发处理之asyncio包使用的详解_Python
Python如何实现可视化热力图
Python实现蒙特卡罗方法(代码示例)
Python映射类型是什么
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 举例说明python中空格是属于字符