1.实例:输出12个星座符号,以反斜线分隔。
for i in range(12): print(chr(9800+i), end="\\")
2.实例:恺撒密码的编码
s=input("请输入明文:") t=ord('a') print("密文是:",end='') for i in s: if t<=ord(i)<=ord('z'): print(chr(t+(ord(i)-t+3)%26),end=' ') else: print(i,end=' ')
3.输入姓名,格式输出:占4位、居中、不足4字的以空格填充。
s=input("输入姓名:") print("输出姓名:{0: ^4}".format(s))
4.格式化输出:中华人民共和国国内生产总值(GDP)689,136.89亿元(2015年)(千分位、2位小数,浮点数)
print("中华人民共和国国内生产总值(GDP){0:,.3f}亿元(2015年)".format(689136.89)
5.打出99乘法表
for x in range(1,10): for y in range(1,x): print('{}*{}={}'.format(x,y,x*y),end=' ') print('\n')
6.下载一首英文的歌词或文章,统计单词出现的次数,将所有,.?!替换为空格,将所有大写转换为小写。
love='''An empty street An empty house A hole inside heart I'm all alone and the rooms Are getting smaller I wonder how I wonder why I wonder where they are The days we had The songs we sang together And oh! my love I'm holding on forever Reaching for a love That seems so far So I say a litter prayer No my dream will take me there Where the skies are blue to see you My love And hope my dream will take me there Where the skies are blue to see you once again my love Overseas from coast to coast Find a place I love the most Where the fields are green To see you once again My love ''' print('My Love'.center(60,'-')) print(love.count('love')) print(love.replace(',',' '))
7.用webbrowser,uweb.open_new_tab('url')打开校园新闻列表
import webbrowser as web web.open_new_tab("www.baidu.com") for i in range(1,3): web.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/'+str(i)+".html")