1. 程式人生 > >字符串操作練習:星座、凱撒密碼、99乘法表、詞頻統計預處理

字符串操作練習:星座、凱撒密碼、99乘法表、詞頻統計預處理

千分位 不足 last 乘法表 控制 ise 精度 end for

實例:輸出12個星座符號,以反斜線分隔。

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

技術分享

愷撒密碼的編碼

sr1="abcdefghijklmnopqrstuvwxyz"
sr2=sr1.upper()
sr=sr1+sr1+sr2+sr2
st=input("please input:")
sR=""
for j in st:
    if j==" ":
        sR = sR +" "
        continue
    i=sr.find(j)
    if(i>-1):
        sR=sR+sr[i-3]
print(sR)

技術分享

{ }裏的格式控制 <序號>:<填充><對齊><寬度><千分位><精度><類型>

print("圓周率{{{1:,.4f}{2}}}是{0}".format("無理數",3.1415926,"..."))

技術分享

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

a=input("please input:")
print(a.center(4, ))

技術分享

中華人民共和國國內生產總值(GDP)
689,136.89億元(2015年)

a=float(input("please input money:
")) b=input("please input year:") print("中華人民共和國國內生產總值(GDP){0:,.2f}億元({1}年)".format(a,b))

技術分享

實例:打出99乘法表

for i in range(1,10):
    for j in range(1,i+1):
        print(j,"*",i,"=",i*j," ",end=‘‘)
    print(end=\n)

技術分享

實例: 下載一首英文的歌詞或文章,統計單詞出現的次數,將所有,.?!替換為空格,將所有大寫轉換為小寫。

sr=‘‘‘Just one last dance ,oh baby just one last dance!
We meet in the night in the Spanish café,
I look in your eyes just don‘t know what to say,
It feels like I‘m drowning in salty water,A few hours left ‘til the sun‘s gonna rise,
tomorrow will come an it‘s time to realize.our love has finished forever,
how I wish to come with you!how I wish we make it through!
Just one last dance!before we say goodbye,
when we sway and turn round and round and round,
it‘s like the first time,Just one more chance,
hold me tight and keep me warm.cause the night is getting cold.
and I don‘t know where I belong,Just one last dance! 
‘‘‘ print("dance出現次數:",sr.count(dance)) for i in sr: sr=sr.replace(,, ) sr=sr.replace(!, ) sr=sr.replace(?, ) print(sr) for i in sr: sr=sr.lower() print(sr)

技術分享

webbrowser打開校園新聞列表

import webbrowser as web
i=input("please input number:")
web.open_new_tab(http://news.gzcc.cn/html/xiaoyuanxinwen/+str(i)+.html)

技術分享

技術分享

字符串操作練習:星座、凱撒密碼、99乘法表、詞頻統計預處理