pythonisnumeric

Using title() function for capitalizing printed sentence


I am looking a way to assign value to printed sentence to capitalize the first letters. The output should be: "Hello World!". This is my current code and the answer is "HELLO NEW WORLD" as a.title capitalizes every letter:

well='13H3e1llo0 312w20orl3d!432'
for a in well:
    if a.isnumeric()==False:
        print(a.title())

Any thoughts?


Solution

  • Here, a.title() works perfectly if we have a='hello world' but here you have used a for loop which means .title() is invoked for each alphabetical character and capitalizes it. We con do this way:

    alphstr = ''
    for char in well:
        if char.isnumeric() == False:
            alphstr += char
    print(alphstr.title())
    

    Hope you find it useful.