加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python代码实例大小写转换,首字母大写,去除特殊字符

发布时间:2020-12-17 17:26:47 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #字母大小写转换#首字母转大写#去除字符串中特殊字符(如:'_','.',',',';'),然后再把去除后的字符串连接起来#去除'hello_for_our_world'中的'_',

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读