小白的自我救赎:Codewars第二天

题目:
Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case).
就是对给定字符串做如下处理:首单词不变,后面的每个单词首字母大写,类似于驼峰命名。

经过我一番苦苦挣扎最后放弃,在网上参考了别人的代码然后自己写的代码如下:

def to_camel_case(text):
	new1=text.replace('-','_')
	new2.new1.split('_')
	if text=='':
		return ''
	else:
		for i in range(len(new2)-1):
			new2[i+1].upper(0)
	return ''.join(new2)

replace()方法:将’-’ 替代为’_'方便后面统一分割
split()方法:通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串,**返回的是分割后的列表

upper()字符串中字母由小写变为大写
lower()字符串中字母由大写变为小写
capitalize()字符串中字母首字母大写其余小写
title()字符串中字母每个单词的首字母大写其余小写

我也不知道为什么我用new[i+1].capitalize()就不对

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值