python - 我正在尝试驼峰式四个短语
问题描述
超级初学者到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()
这是我想要的输出:
purplePeopleEater
iCan’tBelieveIt’sNotButter
heinz57Sauce
我的主要错误信息是invalid character in identifier in line 2
编辑后我的代码运行正常,但输出:
PPE
ICBINB
H5S
解决方案
很简单,只需使用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()
推荐阅读
- javascript - 不使用 windows.print() 打印输入文本框的列表
- assembly - 如何将位范围读入寄存器?
- css - 内联块非替换元素的对齐框
- angular - DatePipe 在 Angular 6 中无法正常工作
- r - 将ggplot对象保存为环境中的图像作为对象/值
- android - 创建 Aar 如何限制我的应用程序仅访问该 aar
- python - New-> IndexError: 索引 3 超出轴 0 的范围,大小为 3
- android - LibGDX 样板代码 NullPointerException
- c# - 使用backgroudWorker C#时出现跨线程操作错误
- python - 如何随机生成变量名?