python-字符串X编码Xformat方法
1.输出12个星座符号,以反斜线分隔。
1 print(ord('郝'))#字符转为编码号 2 print(oct(8800))#八进制 3 for i in range(12): 4 print(chr(9800+i),end='/')#编码号转为字符
2.恺撒密码的编码
1 a=ord('a') 2 s=input('输入你的明文:') 3 print('密文:',end=' ') 4 for i in s: #以我输入的字符串s做一个循环 5 if ord('a')<=ord(i)<=ord('z'): 6 7 print(chr(a+((ord(i)-a+3)%26)),end='') 8 elif ord('0')<=ord(i)<=ord('9'): 9 print(chr(ord('0')+((ord(i)-ord('0')+3)%10)),end='') 10 11 else: 12 print(i,end=' ')
3.输入姓名,格式输出:占4位、居中、不足4字的以空格填充。
1 #{ }里的格式控制 <序号>:<填充><对齐><宽度><千分位><精度><类型> 3 name=input('输入姓名:') 4 print('你的名字:{0:' '^4}'.format(name))
#由于四字居中不明显,所以略微改了一下格式:
1 name=input('输入姓名:') 2 print('你的名字:{0:#^16}'.format(name))
4.格式化输出:中华人民共和国国内生产总值(GDP)689,136.89亿元(2015年)(千分位、2位小数,浮点数)
#format()的{}以0为起点
print('中华人民共和国国内生产总值(GDP):{0:,.2f}亿元({1}年)'.format(689136.89,"2015"))
5.打出99乘法表
for x in range(1,10): for y in range(1,x+1): print('{}x{}={}'.format(x,y,x*y),end=' ') print('\n')
6.下载一首英文的歌词或文章,统计单词出现的次数,将所有,.?!替换为空格,将所有大写转换为小写。
1 #实例: 下载一首英文的歌词或文章,统计单词出现的次数 2 #将所有,.?!替换为空格,将所有大写转换为小写。 3 song=''' 4 Oh woah 5 Oh woah 6 Oh woahohhohh 7 8 You know you love me, I know you care 9 You shout whenever, and I'll be there 10 You want my love, you want my heart 11 And we will never ever ever be apart 12 Are we an item? Girl quit playing 13 We're just friends, what are you saying 14 Said there's another and look right in my eyes 15 My first love broke my heart for the first time 16 And I was like 17 18 Baby, baby, baby, oh like 19 Baby, baby, baby, no like 20 Baby, baby, baby, oh 21 I thought you'd always been mine, mine 22 Baby, baby, baby, oh like 23 Baby, baby, baby, no like 24 Baby, baby, baby, oh 25 I thought you'd always been mine, mine 26 Oh, Oh 27 28 For you, I would have done whatever 29 And I just can't believe we ain't together 30 And I wanna play it cool, but I'm losing you 31 I'll buy you anything, I'll buy you any ring 32 And I'm in pieces, baby fix me 33 And you'll shake me till you wake me from this bad dream 34 I'm going down, down, down, down 35 And I just can't believe my first love won't be around 36 And I'm now like 37 38 Baby, baby, baby, oh like 39 Baby, baby, baby, no like 40 Baby, baby, baby, oh 41 I thought you'd always been mine, mine 42 Baby, baby, baby, oh like 43 Baby, baby, baby, no like 44 Baby, baby, baby, oh 45 I thought you'd always been mine, mine 46 47 When I was 13 I had my first love 48 There was nobody that compared to my baby 49 And nobody came between us or could ever come above 50 She had me going crazy 51 Oh I was starstruck 52 She woke me up daily don't need no starbucks 53 She made my heart pound 54 Asking for a beat when I see her in the street 55 And at school on the playground 56 But I really wanna see her on the weekend 57 She knows she got me dazy 58 Cause she was so amazing 59 And now my heart is breaking 60 But I'll just keep on saying 61 62 Baby, baby, baby, oh like 63 Baby, baby, baby, no like 64 Baby, baby, baby, oh 65 I thought you'd always been mine, mine 66 Baby, baby, baby, oh like 67 Baby, baby, baby, no like 68 Baby, baby, baby, oh 69 I thought you'd always been mine, mine 70 Now I'm all gone 71 Now I'm all gone 72 Now I'm all gone 73 Now I'm all gone 74 I am gone 75 76 ''' 77 print('统计baby出现的次数为'+str(song.count('baby'))+'次')#print内只能输出字符串 78 print('统计Baby出现的次数为'+str(song.count('Baby'))+'次') 79 80 for i in song: 81 song=song.replace(',',' ')#不要忘记写回原文件去,否则一次的修改是会被覆盖的 82 song=song.replace('?',' ') 83 song=song.replace('!',' ') 84 song=song.replace('.',' ') 85 86 print(song) 87 print('转换为小写'.center(50,'=')) 88 89 song=song.lower() 90 print(song) 91 print('统计baby出现的次数为'+str(song.count('baby'))+'次')
7.用webbrowser,uweb.open_new_tab('url')打开校园新闻列表
1 import webbrowser as kok_web 2 kok_web.open_new_tab('www.baidu.com') 3 for i in range(2,5): 4 kok_web.open_new_tab('http://news.gzcc.cn/html/xiaoyuanxinwen/'+str(i)+'.html')