【作业】字符串操作练习:星座、凯撒密码、99乘法表

1、实例:输出12个星座符号,以反斜线分隔。

1 for i in range (12):
2     print(chr(9800+i),end="  \\")

2、实例:恺撒密码的编码。

1 s = input("请输入凯撒的明文密码:")
2 for i in s:
3     if ord("a") <= ord(i) <= ord("z"):
4         print(chr(ord("a") + (ord(i) - ord("a") + 3) % 26),end='')
5     else:
6         print(i,end='')

 

3、输入姓名,格式输出:占4位、居中、不足4字的以空格填充。

1 s = input("请输入姓名(四个字以内):")
2 if len(s) == 4:
3     print("{}".format(s))
4 else:
5     print("{0: ^4}".format(s))

4、格式化输出:中华人民共和国国内生产总值(GDP)689,136.89亿元(2015年)(千分位、2位小数,浮点数)。

1 print('中华人民共和国国内生产总值(GDP):{0:,.2f}亿元(2015年)'.format(689136.89))

5、实例:打出99乘法表。

1 for i in range(1,10):
2     for j in range(1,i+1):
3         print("{}*{}={:2} ".format(j,i,i*j),end ='')
4     print('')

 6、下载一首英文的歌词或文章,统计单词出现的次数,将所有,.?!替换为空格,将所有大写转换为小写。

 1 write='''It was Mother’s Day.Sun Zheng thought he should do something for his mother.He decided to help his mother do some housework.After school he went to a shop to buy some food on his way home.
 2 When he got home,he did his best to cook some nice food,though he couldn’t do the cooking well.Thenhe cleaned the room.He felt very tired,but he was very happy.
 3 When his father and his mother came back and saw the clean rooms and dishes which weren’t so nice,they were very happy.They had their supper together.His mother said,"Thank you,my child!"''' 
 4 write=write.replace('?',' ')
 5 write=write.replace('!',' ')
 6 write=write.replace(',',' ')
 7 write=write.replace('.',' ')
 8 write=write.lower()
 9 print('he计数为:',write.count('he'))
10 print('and计数为:',write.count('and'))
11 print('文章内容:',write)

7、用webbrowser,uweb.open_new_tab('url')打开校园新闻列表。

1 import webbrowser
2 webbrowser.open_new_tab("news.gzcc.cn/html/xiaoyuanxinwen/")

 

posted @ 2017-09-18 15:45  27杨华星  阅读(191)  评论(0编辑  收藏  举报