题目:
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()
就不对