python – HackerRank: Capitalizing the first letter of every word in a string (with arbitrary spacing)

  • remove the unused imports

Your code logic is OK but the execution is not good. Try not to overcomplicate it. Take a piece of paper and write the procedure of how a human like YOU would do it, just with a pencil and paper.

read the names individually... make first character capital if it isn't a digit 

Now that you have basic design, become more specific, or in Python terms

  • read the names individually for word in string.split()
  • make the first character capital: string.title()
  • if it isn’t a digit : if not string(0).isdigit()

The primary problem which I faced is handling arbitrary spaces

string.split() will return the same thing, let it be 1 space or 1000 spaces. It does not matter

Now you have exactly what you need, it is just a matter of putting it together.

for every word in words, capitalize if the first character isn't a digit else do nothing
def solve(s):
    return " ".join(word.title() if not word(0).isdigit() else word for word in words.split())