Computer Science
Consider the following string mySubject:
mySubject = "Computer Science"
What will be the output of the following string operations?
(i) print (mySubject [0:len(mySubject)])
(ii) print (mySubject[-7:-1])
(iii) print (mySubject[::2])
(iv) print (mySubject [len (mySubject) -1])
(v) print (2*mySubject)
(vi) print (mySubject[::-2])
(vii) print (mySubject[:3] + mySubject[3:])
(viii) print (mySubject.swapcase() )
(ix) print (mySubject.startswith('Comp') )
(x) print (mySubject.isalpha() )
Python String Manipulation
7 Likes
Answer
(i) Computer Science
(ii) Scienc
(iii) Cmue cec
(iv) e
(v) Computer ScienceComputer Science
(vi) eniSrtpo
(vii) Computer Science
(viii) cOMPUTER sCIENCE
(ix) True
(x) False
Explanation
(i) It slices the string from index 0 to the length of the string, which returns the entire string.
(ii) It uses backward slicing to extract a substring starting from the 7th character from the end to the second-to-last character.
(iii) It slices the string by taking every second character starting from index 0.
(iv) The code first calculates the length of the string mySubject, which is 16. It then accesses the character at the index len(mySubject) - 1, which is 15, corresponding to the last character of the string, 'e'.
(v) It multiplies the string mySubject by 2, which duplicates the string.
(vi) It slices the string mySubject with a step of -2, which reverses the string and selects every second character.
(vii) The code print(mySubject[:3] + mySubject[3:]) combines two slices of the string mySubject. mySubject[:3] extracts the first three characters, and mySubject[3:] extracts the substring from the fourth character to the end. Concatenating these substrings prints 'Computer Science'.
(viii) The swapcase() function converts all uppercase letters in the string mySubject to lowercase and all lowercase letters to uppercase.
(ix) The startswith() function checks if the string mySubject starts with the substring 'Comp'. It returns True because mySubject begins with 'Comp'.
(x) The isalpha() function checks if all characters in the string mySubject are alphabetic. It returns False because the string contains spaces, which are not alphabetic characters.
Answered By
1 Like
Related Questions
Write the output of the following:
s = "Strings in Python" print(s.capitalize()) print(s.title()) s6 = s.replace("in", "data type") print(s6)Find the output of the following:
word = 'work hard' result = word.find('work') print("Substring, 'work', found at index:", result) result = word.find('har') print("Substring, 'har ' ,found at index:", result) if (word.find('pawan') != -1): print("Contains given substring ") else: print("Doesn't contain given substring")Write the Python statement and the output for the following:
(a) Find the third occurrence of 'e' in 'sequence'.
(b) Change the case of each letter in string 'FuNcTioN'.
(c) Whether 'Z' exists in string 'School' or not.
Consider the string str1 = "Global Warming".
Write statements in Python to implement the following:
(a) To display the last four characters.
(b) To replace all the occurrences of letter 'a' in the string with "*".