中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

Python 練習(xí)實(shí)例17

Python 100例 Python 100例

題目:輸入一行字符,分別統(tǒng)計(jì)出其中英文字母、空格、數(shù)字和其它字符的個數(shù)。

程序分析:利用 while 或 for 語句,條件為輸入的字符不為 'n'。

實(shí)例 - 使用 while 循環(huán)

#!/usr/bin/python # -*- coding: UTF-8 -*- import string s = raw_input('請輸入一個字符串:n') letters = 0 space = 0 digit = 0 others = 0 i=0 while i < len(s): c = s[i] i += 1 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,others)

實(shí)例 - 使用 for 循環(huán)

#!/usr/bin/python # -*- coding: UTF-8 -*- import string s = raw_input('請輸入一個字符串: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,others)

以上實(shí)例輸出結(jié)果為:

請輸入一個字符串:
123jsonc  kdf235*(dfl
char = 13,space = 2,digit = 6,others = 2

Python 100例 Python 100例

其他擴(kuò)展