python開發_大小寫轉換,首字母大寫,去除特殊字元

來源:互聯網
上載者:User

標籤:

這篇blog主要是總結我們在平常開發過程中對字串的一些操作:

#字母大小寫轉換#首字母轉大寫#去除字串中特殊字元(如:‘_‘,‘.‘,‘,‘,‘;‘),然後再把去除後的字串串連起來#去除‘hello_for_our_world‘中的‘_‘,並且把從第一個‘_‘以後的單字首大寫

具體的代碼demo:

 1 #字母大小寫轉換 2 #首字母轉大寫 3 #去除字串中特殊字元(如:‘_‘,‘.‘,‘,‘,‘;‘),然後再把去除後的字串串連起來 4 #去除‘hello_for_our_world‘中的‘_‘,並且把從第一個‘_‘以後的單字首大寫 5 low_strs = ‘abcd‘ 6 uper_strs = ‘DEFG‘ 7 test_strA = ‘hello_world‘ 8 test_strB = ‘goodBoy‘ 9 test_strC = ‘hello_for_our_world‘10 test_strD = ‘hello__our_world_‘11 12 #小寫轉大寫13 low_strs = low_strs.upper()14 print(‘abcd小寫轉大寫:‘, low_strs)15 16 #大寫轉小寫17 uper_strs = uper_strs.lower()18 print(‘DEFG大寫轉小寫:‘, uper_strs)19 20 #只大寫第一個字母21 test_strB = test_strB[0].upper() + test_strB[1:]22 print(‘goodBoy只大寫第一個字母:‘, test_strB)23 24 #去掉中間的‘_‘,其他符號都是可以的,如:‘.‘,‘,‘,‘;‘25 test_strA = ‘‘.join(test_strA.split(‘_‘))26 print(‘hello_world去掉中間的\‘_\‘:‘, test_strA)27 28 #去除‘hello_for_our_world‘中的‘_‘,並且把從第一個‘_‘以後的單字首大寫29 def get_str(oriStr,splitStr):30     str_list = oriStr.split(splitStr)31     if len(str_list) > 1:32         for index in range(1, len(str_list)):33             if str_list[index] != ‘‘:34                 str_list[index] = str_list[index][0].upper() + str_list[index][1:]35             else:36                 continue37         return ‘‘.join(str_list)38     else:39         return oriStr40 41 print(‘去除\‘hello_for_our_world\‘中的\‘_\‘,並且把從第一個\‘_\‘以後的單字首大寫:‘, get_str(test_strC,‘_‘))42 print(‘去除\‘hello__our_world_\‘中的\‘_\‘,並且把從第一個\‘_\‘以後的單字首大寫:‘, get_str(test_strD,‘_‘))

運行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> abcd小寫轉大寫: ABCDDEFG大寫轉小寫: defggoodBoy只大寫第一個字母: GoodBoyhello_world去掉中間的‘_‘: helloworld去除‘hello_for_our_world‘中的‘_‘,並且把從第一個‘_‘以後的單字首大寫: helloForOurWorld去除‘hello__our_world_‘中的‘_‘,並且把從第一個‘_‘以後的單字首大寫: helloOurWorld>>> 

python開發_大小寫轉換,首字母大寫,去除特殊字元

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.