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

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

posted @ 2013-07-28 21:22  Hongten  阅读(27820)  评论(2编辑  收藏  举报
Fork me on GitHub