您好,欢迎访问一九零五行业门户网

python开发

总结我们在平常开发过程中对字符串的一些操作:
#字母大小写转换
#首字母转大写
#去除字符串中特殊字符(如:'_','.',',',';'),然后再把去除后的字符串连接起来
#去除'hello_for_our_world'中的'_',并且把从第一个'_'以后的单词首字母大写
代码实例:
#字母大小写转换 #首字母转大写 #去除字符串中特殊字符(如:'_','.',',',';'),然后再把去除后的字符串连接起来 #去除'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
>>>
其它类似信息

推荐信息