PYTHON - Explain the code with comments for each step (so I can understand the c
ID: 3587666 • Letter: P
Question
PYTHON - Explain the code with comments for each step (so I can understand the concept). Thanks
def word2capitalize (s): "" TTo take user input and print out that string in the Title Case That is, the first letter of each word is uppercase and the remainder of the word is lowercase. If the entire word is two letters long, the entire word is lower case. The three letter words "and,""the," and "not" are all lowercase while all other three letter wordsare capitalized. The first word in the string is capitalized, no matter how long it is. Test data: input: 'the quick brown dogNow is the time for all good men' And to all a good night' output: 'The Quick Brown Dog Now is the Time For All Good Men'i And to All A Good Night' Params: s (string) Returns: (3 strings) IT skipList= { ' and ' ' ' the ' , ' not ' } sen- s . split ( ' ' ) sentence = ' ' for word in sen: if word not in skipList and len (word) > 2 or len (word) is 1: elif word in skipList and word[0].islower): else: sentence-sentence+''+ word.title () sentence-sentence+''+ word.title () sentence - sentence + ''+ word return sentence.lstrip() s = input ("Type in any string :") sen -word2 capitalize (s) print (sen)Explanation / Answer
Here the actualy program start from
s = input("Type in Any string:").
This line is will take the input from the user and stores it in variable 's'. then the control goes to second line
sen = word2capitalize(s)
Here we are passing the input taken from the user to a function/method called 'word2capitalize' which will give us the capitalized sentence as per the logic written in that fucntion. So now the control goes to top of the program where function starts.
def word2capitalize(s)
The word 'def' is standard defination of a function in python. 'word2capitalize(s)' is the user defined function name with a parameter 's'.
skiplist = {'and','the','not'}
Here we are creating a list of words which we would like to skip from capitalization process.
sen = s.split(' ')
This will split the sentence 's' which is passed to this function in to multiple parts using '<space>' as a delimiter.
ex: hi hello there --> 'hi' 'hello' 'there'
sentence = ''
Here we are creating a variable in order to store the desired output of the function.
for word in sen:
This will iterate throw each word in 'sen', means in our above example each word will be loaded into variable 'word' in each iteration.
if word not in skiplist and len(word)>2 or len(word) is 1:
We are cheking whether the word under process is in skip list and the length of the word is greater than 2 or equal to 1. This means if a word is not in skip list and the length is equal to 1 or greater than 2 then that word will pass the if condition and goes inside.
sentence = sentence + ' ' +word.title()
Here we are changing the first letter of the word which is under process into to capital form. and adding that word to 'sentence' variable
else if word in skiplist and word[0].islowercase():
If the first IF condition fails then the controls goes to next IF condition which is if the word is in skip list and first word of the user input is in lower case, i.e. in our example 'hi'
sentence = sentence + ' ' +word.title()
Then also the word will get converted to title format and will ger added to the 'sentence' variable with a space in between.
else:
If the word fails both conditions then the flow will goto else part of the function.
sentence = sentence +' '+word
we will add the word as it is to the sentence vairable.
return sentence.lstrip()
And finally we will return the sentence variable to the main flow of the program by removing any whitespace characters in the sentence using the inbuilt string function (lstrip())
sen = word2capitalize(s)
Now the variable sen will have modified version of the user input as the rules we defined in the function.
print(sen)
Finally we will print the sentence.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.