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
-
Answer # 2
First let's understand what a function is
ord()
. It returns the numeric representation for the specified character. Those. if we take for example the symbolb
-then in numerical representation it will be -98
subtract 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 character
A
, i.e.:1 + 65= 66 66=B
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable
First let's understand what a function is
ord()
. It returns the numeric representation for the specified character. Those. if we take for example the symbolb
-then in numerical representation it will be -98
subtract from this number the numerical value of the symbola
, according to the table it is clear that this is -97
, we get the following:Add the numeric representation of the character
A
, i.e.: