Capitalize!

  • + 28 comments

    Dear NamanRastogi, thanks a lot for your thoughtful comment.


    Let me kindly provide some feedback. The code above is a bit inefficient as replace() method inside the loop creates copies of string. The reason is that a string is an immutable object. So it's better first to translate it into a mutable object, process the mutable one inside the loop and finally make a backward transformation.


    Let me provide an example.

    a_string = input().split(' ')
    print(' '.join((word.capitalize() for word in a_string)))

    • In the first line a string is transformed to a mutable list
    • split() with a specified separator doesn't group consecutive whitespaces (please kindly refer to split() doc)