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

  • 实例:输出12个星座符号,以反斜线分隔。
for i in range (12):
    print(chr(9800+i),end='\\')

  输出结果:

  • 实例:恺撒密码的编码
s = input('请输入需要加密的字符串:')
key = int(input('请输入加密位数:'))
print("加密后:",end='')
for i in s:
    if ord('a')<=ord(i)<=ord('z'):
        print(chr(ord('a')+((ord(i)+key+7)%26)),end='')
    else:
        print(i,end='')

  输出结果:

  • 输入姓名,格式输出:占4位、居中、不足4字的以空格填充。
s=input("请输入姓名:")
print("{0: ^4}".format(s))

  输出结果:

  • 格式化输出:中华人民共和国国内生产总值(GDP)689,136.89亿元(2015年)(千分位、2位小数,浮点数)
print("中华人民共和国国内生产总值(GDP){:,.2f}亿元(2015年)".format(689136.89))

  输出结果:

  • 实例:打出99乘法表
for x in range(1,10):
    for y in range(1,x+1):
        print("{}*{}={}".format(y,x,x*y),end=' ')
    print()

  输出结果:

  • 实例: 下载一首英文的歌词或文章,统计单词出现的次数,将所有,.?!替换为空格,将所有大写转换为小写。
faded = '''You were the shadow to my light
Did you feel us?
Another start
You fade away
Afraid our aim is out of sight
Wanna see us
Alive
Where are you now?
Where are you now?
Where are you now?
Was it all in my fantasy?
Where are you now?
Were you only imaginary?
Where are you now?
Atlantis
Under the sea
Under the sea
Where are you now?
Another dream
The monster's running wild inside of me
I'm faded
I'm faded
So lost, I'm faded
I'm faded
So lost, I'm faded
These shallow waters never met what I needed
I'm letting go a deeper dive
Eternal silence of the sea. I'm breathing alive
Where are you now?
Where are you now?
Under the bright but faded lights
You've set my heart on fire
Where are you now?
Where are you now?
Where are you now?
Atlantis
Under the sea
Under the sea
Where are you now?
Another dream
The monster's running wild inside of me
I'm faded
I'm faded
So lost, I'm faded
I'm faded
So lost, I'm faded'''
faded=faded.replace('?',' ')
faded=faded.replace('',' ')
faded=faded.replace(',',' ')
faded=faded.replace('.',' ')
faded=faded.replace('\'',' ')
faded=faded.lower()
print("faded次数统计:",faded.count('faded'))
print("\n转换后歌词:\n",faded)

  输出结果:

 

  • 用webbrowser,uweb.open_new_tab('url')打开校园新闻列表
import webbrowser as web
for i in range(10,12):
    web.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/'+str(i)+'.html')

 

 

posted @ 2017-09-18 16:42  12-张振勋  阅读(337)  评论(0编辑  收藏  举报