Capitalize!

Sort by

recency

|

2926 Discussions

|

  • + 0 comments

    .title cant working instant can use .split()

  • + 0 comments

    While solving this problem, I struggled for a long time on exactly what I did wrong, here I just want to discuss a few tricks I learned while solving!

    First off, .title() method does not work!

    .title() method makes the first letter of each word uppercase, even if it is not the first character! For example, "123abc" will become "123Abc"

    And according to the problem, "Note: in a word only the first character is capitalized. Example "12abc" when capitalized remains "12abc"", thus .title() method does not work!

    A better alternative would be the .capitalize() method. It makes the first character of a word uppercase (if it is a letter). For example, "123abc" will stay as "123abc", while "abcde" will become "Abcde"

    Secondly, be careful of the difference between .split() and .split(" ")

    .split() method splits a string at whitespaces, but does not preserve the whitespaces! For example, "abc def ghi" (there should be 3 whitespaces between def and ghi, but HackrRank automatically merged it into 1 whitespace) will become ["abc", "def", "ghi"]

    On the other hand, .split(" ") method also splits a string at whitespaces, but preserves all whitespaces! For example, "abc def ghi" (there should be 3 whitespaces between def and ghi, but HackrRank automatically merged it into 1 whitespace) will become ["abc", " ", "def", " ", " ", " ", "ghi"]

    Lastly, remember that strings are immutable objects!

    (P.S. Please let me know if you think I am wrong on any part!)

  • + 0 comments

    Easy clean solution def solve(s):

    new_list = s.split(" ")
    new_list = map(str.capitalize,new_list)
    return " ".join(new_list)
    
  • + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    # Complete the solve function below.
    def solve(s):
        s = re.split(r'(\s)', s)
        s = [l.capitalize() for l in s]
        cadena = ""
        for l in s:
            cadena = cadena + l 
    
        return str(cadena)
        
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        s = input()
    
        result = solve(s)
    
        fptr.write(result + '\n')
    
        fptr.close()
    
  • + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    # Complete the solve function below.
    def solve(s):
        fullname=""
        li=re.split('(\s+)',s)
        for i  in li:
            if i.isalpha():
                fullname=fullname+i.title()
            else:
                fullname=fullname+i
        return fullname
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        s = input()
    
        result = solve(s)
    
        fptr.write(result + '\n')
    
        fptr.close()