愚蠢的切片并在字符串中附加问题

时间:2019-06-10 20:22:21

标签: python

我正在做一些练习,想将“ macdonald”更改为“ MacDonald”。但是,我的输出仅显示最后一个值为“ True”的值,即“ D”。如何让程序显示全文?

name = 'macdonald'

def myfunc(name):

    for x in name:
        finalName = ""
        if x == name[0] or name[3]:
            finalName += x.capitalize()
        else:
            finalName += x

    return finalName

1 个答案:

答案 0 :(得分:2)

name = 'macdonald'

def myfunc(name):

    finalName = ""
    for i, x in enumerate(name):
        if i == 0 or i == 3:
            finalName += x.capitalize()
        else:
            finalName += x

    return finalName

print(myfunc(name))