温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python字符串转换大小写的方法

发布时间:2020-07-17 14:45:10 来源:亿速云 阅读:227 作者:清晨 栏目:编程语言

小编给大家分享一下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字符串转换大小写的方法有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI