mis*_*lub 2 python camelcasing
超级初学者到Python这里。到目前为止,这是我的代码:
def make_camel_case(word_string):
word_list = word_string.split(’ ’)
output = ’’
for word in word_list:
word_upper = word[0].upper()
output = output+word_upper
return(output)
def camel_case():
phrase1 = ’purple people eater’
phrase2 = ’i can\’t believe it\’s not butter’
phrase3 = ’heinz 57 sauce’
print(make_camel_case(phrase1))
print(make_camel_case(phrase2))
print(make_camel_case(phrase3))
camel_case()
Run Code Online (Sandbox Code Playgroud)
这是我想要的输出:
purplePeopleEater
iCan’tBelieveIt’sNotButter
heinz57Sauce
Run Code Online (Sandbox Code Playgroud)
我的主要错误信息是 invalid character in identifier in line 2
编辑后我的代码运行正常,但输出:
PPE
ICBINB
H5S
Run Code Online (Sandbox Code Playgroud)
简单点,只需使用capitalize()函数:
def make_camel_case(word_string):
word_list = word_string.split(' ')
output = ''
for word in word_list:
word_upper = word.capitalize()
output += word_upper
return output
def camel_case():
phrase1 = 'purple people eater'
phrase2 = "i can't believe it’s not butter"
phrase3 = 'heinz 57 sauce'
print(make_camel_case(phrase1))
print(make_camel_case(phrase2))
print(make_camel_case(phrase3))
camel_case()
Run Code Online (Sandbox Code Playgroud)
小智 5
第二行引起的错误是由无效字符 '. 您应该使用单引号 ' ' 或双引号 " " 按空格分隔文本。
然后你可以简单地对每个单词的每个第一个字母执行 .upper() 使其大写,最后,将所有单词合并到所需的字符串中。
| 归档时间: |
|
| 查看次数: |
89 次 |
| 最近记录: |