字符串操作练习:星座、凯撒密码、99乘法表、词频统计预处理

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

 

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

 

 

实例二:恺撒密码的编码

plaincode=input("明文:")
print("密文:")
x=ord('A')
y=ord('Z')
i=ord('a')
j=ord('z')
for k in plaincode:
    if x<= ord(k) <=y:
        print(chr(x+(ord(k)-x+3)%26),end='')
    elif i<= ord(k) <=j:
        print(chr(i+(ord(k)-i+3)%26),end='')
    else:
        print(k,end='')

 

 

 

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

 

name = input("请输入你的名字:")
print("格式输出:{0: ^4}".format(name))

 

 

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

 

year = input("请输入年份:")
gdp = float(input("请输入{}年的GDP:".format(year)))
print("中华人民共和国国内生产总值(GDP){0:,.2f}亿元({1}年)".format(gdp,year))

 

 

实例五:打出99乘法表

 

for i in range(1,10):
    for j in range(1,10):
        print("{}x{}={}".format(i,j,i*j),end=' ')
        if i==j:
            print("\n")
            break

 

 

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

 

s = '''Living in the 21st century offers certain advantages,such as a higher
standard of living, but it also has some disadvantages, such as a polluted envir
onment.To begin with, most people now have more money for less hard work. They e
arn higher salaries than before and enjoy better social security, such as social
welfare for laid-off workers and disability insurance.Secondly, because of the a
dvance in medical technology which leads to better medical care and treatment,
people’s life expectancy is longer. Moreover, most people now can afford to buy
foods of high nutrition and enjoy their leisure time.'''
print(s)
word = input("请输入查询的单词:")
print("该单词出现的次数为:%d"%str(s).count(word))
for i in s:
    s=str(s).replace(","," ")
    s=str(s).replace("."," ")
    s=str(s).replace("?"," ")
    s=str(s).replace("!"," ")
    s=str(s).lower()
print(s)

 

 

实例七:用webbrowser,uweb.open_new_tab('url')打开校园新闻列表

 

import webbrowser as web
web.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/')

 

 

posted on 2017-09-18 18:42  ZJQ-013  阅读(219)  评论(0编辑  收藏  举报