• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python字符串大小写如何转换

python 搞java代码 3年前 (2022-05-21) 30次浏览 已收录 0个评论

平常开发过程中对字符串的一些操作:

#字母大小写转换
#<a href="https://www.gaodaima.com/tag/%e9%a6%96%e5%ad%97%e6%af%8d" title="查看更多关于首字母的文章" target="_blank">首字母</a>转大写
#<a href="https://www.gaodaima.com/tag/%e5%8e%bb%e9%99%a4" title="查看更多关于去除的文章" target="_blank">去除</a>字符串中特殊字符(如:'_','.',',',';'),然后再把去除后的字符串连接起来
#去除'hello_for_our_world'中的'_',并且把从第一个'_'以后的单词首字母大写

www#gaodaima.com来源gaodai$ma#com搞$$代**码网搞代码

代码实例:

#字母大小写转换
#首字母转大写
#去除字符串中特殊字符(如:'_','.',',',';'),然后再把去除后的字符串连接起来
#去除'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教程。

来源:搞代码网:原文地址:https://www.gaodaima.com


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python字符串大小写如何转换
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址