python开发_大小写转换,首字母大写,去除特殊字符

转自:https://www.weidianyuedu.com/

总结我们在平常开发过程中对字符串的一些操作:

#字母大小写转换

#首字母转大写

#去除字符串中特殊字符(如:‘_’,‘.’,‘,’,‘;’),然后再把去除后的字符串连接起来

#去除’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)
https://www.weidianyuedu.com/

#大写转小写
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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值