python开发_大小写转换,首字母大写,去除特殊字符
2013-07-28 21:22
525 查看
这篇blog主要是总结我们在平常开发过程中对字符串的一些操作:
具体的代码demo:
运行效果:
#字母大小写转换 #首字母转大写 #去除字符串中特殊字符(如:'_','.',',',';'),然后再把去除后的字符串连接起来 #去除'hello_for_our_world'中的'_',并且把从第一个'_'以后的单词首字母大写
具体的代码demo:
#字母大小写转换 #首字母转大写 #去除字符串中特殊字符(如:'_','.',',',';'),然后再把去除后的字符串连接起来 #去除'hello_for_our_world'中的'_',并且把从第一个'_'以后的单词首字母大写
low_strs = 'abcd'
uper_strs = 'DEFG'
test_strA = 'hello_world'
test_strB = 'goodBoy'
test_strC = 'hello_for_our_world'
test_strD = 'hello__our_world_'
#小写转大写
low_strs = low_strs.upper()
print('abcd小写转大写:', low_strs)
#大写转小写
uper_strs = uper_strs.lower()
print('DEFG大写转小写:', uper_strs)
#只大写第一个字母
test_strB = test_strB[0].upper() + test_strB[1:]
print('goodBoy只大写第一个字母:', test_strB)
#去掉中间的'_',其他符号都是可以的,如:'.',',',';'
test_strA = ''.join(test_strA.split('_'))
print('hello_world去掉中间的\'_\':', test_strA)
#去除'hello_for_our_world'中的'_',并且把从第一个'_'以后的单词首字母大写
def get_str(oriStr,splitStr):
str_list = oriStr.split(splitStr)
if len(str_list) > 1:
for index in range(1, len(str_list)):
if str_list[index] != '':
str_list[index] = str_list[index][0].upper() + str_list[index][1:]
else:
continue
return ''.join(str_list)
else:
return oriStr
print('去除\'hello_for_our_world\'中的\'_\',并且把从第一个\'_\'以后的单词首字母大写:', get_str(test_strC,'_'))
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 >>>
相关文章推荐
- python开发_大小写转换,首字母大写,去除特殊字符
- python开发_大小写转换,首字母大写,去除特殊字符
- python 输入一个字符,是小写转换为大写,大写转换为小写,其他字符原样输出
- 去除字符串中的特殊字符,只保留数字、字母或下划线
- 大小写转换,去除连字符
- 大小写转换,去除连字符
- PHP程序开发之字母大小写转换相关函数介绍
- 大小写转换,去除连字符
- 大小写转换,去除连字符
- PHP 大小写转换、首字母大写、每个单词首字母大写转换相关函数
- python里使用capwords()函数来把字符里每一个英语单词首字母变大写
- XML Publish开发--特殊字符转换
- 大小写转换,去除连字符
- 大小写转换,去除连字符
- 密码同时包含6~18位数字和大小写字母,不包含特殊字符的判断方法(正则表达式)
- swift 字母大小写转换代码,大写转小写,小写转大写都是一行
- 大小写转换,去除连字符
- 大小写转换,去除连字符
- 转换字符把每个字符串的首字母转换大写
- php将字符首字母大小写转换