Home>

Big Letters Challenge

Write a capitalize() function that accepts a word from small Latin letters and returns it, changing the first letter to a capital one.

def capitalize(word):
    first_letter_small= word[0]
    first_letter_big= chr(ord(first_letter_small) -ord('a') + ord('B'))
    return first_letter_big + word[1:]
source= input().split()
res= []
for word in source:
    res.append(capitalize(word))
print(' '.join(res))

as I understand it (line by line from the input):

1 first enter 2 words separated by space e.g. jabra kadabra

2 creates an empty res list

3 runs a for loop to stuff the list

4 res.append(capitalize(word)) runs the function

in a function:

5 extract the first letter of the first word

6 ??? the first letter was removed and replaced with a large one, but I don't understand chr(ord(first_letter_small) -ord('a') + ord('A')):

ord(first_letter_small) is the letter j minus ord('a') + ord('A')) (why a and A, or python understands that you need to take not "a" and "A" (or 'z' 'Z'), but ord(first_letter_small), i.e. 'j' if you substitute 'a' 'B', a shift occurs: characters 1 more in the alphabet will be substituted

7 return the first capital attached to the rest of the word

??? since the words are 2, the function is automatically called 2 times?

  • Answer # 1

    First let's understand what a function isord(). It returns the numeric representation for the specified character. Those. if we take for example the symbolb-then in numerical representation it will be -98subtract from this number the numerical value of the symbola, according to the table it is clear that this is -97, we get the following:

    98 -97= 1
    

    Add the numeric representation of the characterA, i.e.:

    1 + 65= 66
    66=B
    
  • Answer # 2

    First let's understand what a function isord(). It returns the numeric representation for the specified character. Those. if we take for example the symbolb-then in numerical representation it will be -98subtract from this number the numerical value of the symbola, according to the table it is clear that this is -97, we get the following:

    98 -97= 1
    

    Add the numeric representation of the characterA, i.e.:

    1 + 65= 66
    66=B