파이썬은 운동 (17)
제목 : 문자의 라인을 입력, 각각 통계있는 영어 문자, 공백, 숫자 및 다른 문자의 수.
분석 프로그램 : while 문을 사용하여, 조건은 'N \'입력 문자가 아닙니다.
소스 코드 :
#!/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,others)
위 예제의 출력은 다음과 같습니다
input a string: w3big char = 6,space = 0,digit = 0,others = 0