python隨筆02

來源:互聯網
上載者:User

標籤:code   分割   置中   print   lower   bsp   highlight   spl   int   

1.改變字串的首字母為大寫

 

s = ‘welcome to bokeyuan‘s1 = s.capitalize()print ()

 

2.將字串全部變為大小寫

s = ‘Welcome To Bokeyuan‘s1 = s.capitalize()s2 = s.upper()s3 = s.lower()print(s2)print(s3)

3.將字元置中,可以添加填充來代替

s = ‘menu‘s4 = s.center(20)s5 = s.center(20,‘-‘)print(s4)print(s5)

>>>

menu 

--------menu--------


 

4.查看字串的長度

s = ‘我喜歡讀書,Thank‘s6 = len(s)print(s6)>>>
14 #標點也算一位

5.指定分割符,來分割指定的字元

s = ‘welcome to bokeyuan‘s10 = s.split(‘e‘)print(s10) >>> 
[‘w‘, ‘lcom‘, ‘ to bok‘, ‘yuan‘] #結果為列表,並且不包含指定分割的分割符

6.替換

s = ‘我叫小米,我愛我的小米小米‘s11 = s.replace(‘小米‘,‘大米‘)s12 = s.replace(‘小米‘,‘大米‘,1)s13 = s.replace(‘小米‘,‘大米‘,2)     #s12,s13 後跟的數字不同替換的個數不同print(‘s11:‘,s11)print(‘s12:‘,s12)print(‘s13:‘,s13)>>>s11: 我叫大米,我愛我的大米大米s12: 我叫大米,我愛我的小米小米s13: 我叫大米,我愛我的大米小米

7.format的使用

name = input(‘請輸入你的名字:  ‘)age= input(‘請輸入你的年齡:  ‘)hobby= input(‘請輸入你的愛好:  ‘)Introduce = ‘我叫{},我的年齡是{},我的愛好是{}‘.format(name,age,hobby)print(Introduce)>>>請輸入你的名字:  Lucy請輸入你的年齡:  18請輸入你的愛好:  Dancing我叫Lucy,我的年齡是18,我的愛好是Dancing

  

python隨筆02

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.