標籤:
這篇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開發_大小寫轉換,首字母大寫,去除特殊字元