欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python 字符串大小写转换 其它不变_python字符串大小写如何转换

发布时间:2024/7/23 python 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python 字符串大小写转换 其它不变_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 indexin 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教程。

总结

以上是生活随笔为你收集整理的python 字符串大小写转换 其它不变_python字符串大小写如何转换的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。