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


這篇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                 continue
37         return ''.join(str_list)
38     else:
39         return oriStr
40 
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 win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
abcd小寫轉大寫: ABCD
DEFG大寫轉小寫: defg
goodBoy只大寫第一個字母: GoodBoy
hello_world去掉中間的'_': helloworld
去除'hello_for_our_world'中的'_',並且把從第一個'_'以后的單詞首字母大寫: helloForOurWorld
去除'hello__our_world_'中的'_',並且把從第一個'_'以后的單詞首字母大寫: helloOurWorld
>>> 

 

========================================================

More reading,and english is important.

I'm Hongten

 

大哥哥大姐姐,覺得有用打賞點哦!多多少少沒關系,一分也是對我的支持和鼓勵。謝謝。
Hongten博客排名在100名以內。粉絲過千。
Hongten出品,必是精品。

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

========================================================


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM