python习题

1.求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。

t=0
a=input("输入一个整数")
b=input("再输入一个整数")
print(a,b)
for i in range(1,int(b)+1):
    t+=int(a*i)
    print(a*i)
print(t)

2.输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

import string

s = input("shuru xxxx:")
letters=0
space=0
digit=0
others=0

for i in s:
    if i.isalpha():
        letters+=1
    elif i.isspace():
        space+=1
    elif i.isdigit():
        digit+=1
    else:
        others+=1
print(f'char = {letters},space={space} dight={digit},other = {others}')

3.输出指定格式的日期

import datetime

print(datetime.date.today().strftime('%d/%m/%Y'))
a=datetime.date(1989,2,2)
print(a.strftime('%d-%m-%Y'))
a=a+datetime.timedelta(days=1)
print(a.strftime('%d-%m-%Y'))
a=a.replace(year=a.year+1)
print(a.strftime('%Y'))

你可能感兴趣的:(python习题)