統計字串以及列印乘法口訣表

小周啊發表於2019-05-12

1、統計字串中有多少個數字、字母、空格以及其他字元

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2018/1/24 21:29
# @Author  : zhouyuyao
# @File    : countnums.py
# PyCharm 2017.3.2 (Community Edition)
# Build #PC-173.4127.16, built on December 19, 2017
# JRE: 1.8.0_152-release-1024-b8 amd64
# JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
# Windows 10 10.0
# Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) 
# [MSC v.1900 64 bit (AMD64)] on win32

status=1
while status:
    strings = input("Please input a string(`quit` will exit):")
    if strings == "quit":
        exit(1)
    digit = pha = space = other = 0
    for i in strings:
        if i.isdigit():
            digit +=1
        elif i.isalpha():
            pha +=1
        elif i.isspace():
            space +=1
        else:
            other +=1
    print("該字串中數字有{0}個,字母有{1}個,空格有{2}個,其他{3}個。".format(digit,pha,space,other))

2、列印乘法口訣表

for i in range(1,10):
    for j in range(1,i+1):
        print("{0} x {1} = {2}".format(j,i,i*j),end="")
        if i ==j:
            print("")
1 x 1 = 1
1 x 2 = 22 x 2 = 4
1 x 3 = 32 x 3 = 63 x 3 = 9
1 x 4 = 42 x 4 = 83 x 4 = 124 x 4 = 16
1 x 5 = 52 x 5 = 103 x 5 = 154 x 5 = 205 x 5 = 25
1 x 6 = 62 x 6 = 123 x 6 = 184 x 6 = 245 x 6 = 306 x 6 = 36
1 x 7 = 72 x 7 = 143 x 7 = 214 x 7 = 285 x 7 = 356 x 7 = 427 x 7 = 49
1 x 8 = 82 x 8 = 163 x 8 = 244 x 8 = 325 x 8 = 406 x 8 = 487 x 8 = 568 x 8 = 64
1 x 9 = 92 x 9 = 183 x 9 = 274 x 9 = 365 x 9 = 456 x 9 = 547 x 9 = 638 x 9 = 729 x 9 = 81


相關文章