(Strings in Python)
(Strings in Python)
>> print (x[1:-2]) 5. Write the output of the following: "Hello Madam, I love Tutorials" >>> x (a) string substring = "Madam" if string. find(substring) !=-1: print ("Python found the substring! else: print ("Python did NOT find the substring! (b) s print(s.capitalize()) print(s.title(}) s6=s.replace("in", "data type") print(s6) ‘Strings in Python" 6. 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" Consider the following string mySubject: mySubject = "Computer Science" What will be the output of the follo i string operations? (i) print (mySubject [0: len (mySubject) }) (ii) print (mysubject [-7:-1]) (ii) print (mySubject (::2]) (iv) print (mysubject [1en (mySubject)-1]) (v) print (2*mysubject) (vi) print (mySubject [::-2]) (wi) print (mysubject [:3] + mySubject [3:]) (vii) print (mysubject.. swapcase()) (x) print (mysubject.startswith (*Comp') ) (¥) print (mysubject .isalpha())tne thon statement and the output for the ‘& wed the third occurrence of ‘ein ‘sequence’, (a) change the case of each letter in string ‘FuNcTion’ OO) peter 2 exists In sting ‘Schoo!’ of not. : the string str1="Global Warming”, ents in Python to implement the following: ete ply the ast four characters e To replace all the occurrences of letter ‘ain the string with ith “00 output of the following program code: . tyirus-virus-virus! 4 following: ( idet consi 9, co tte yo write th ae . tr ["Fundamentals", "of", "Python") =m" #no space ai print (d1-Jo!n ({str)) gy, What will be the output of the following code? qext = "Mind@Work!" ja = len(Text) aText = "" for i in range(0,1n): if Text [i] -isupper(): nfext = nText + Text[i].lower() elif Text[i] .isalpha/( nText + Text (i].upper() " 4 nText else: nfext + 'A' nText print (nText) tninputthe string ‘My School’. Write a script to partition the string atthe occurrence of letter W. 12. Wite a program to convert a string with more than one word into titlecase string where string is passed asparameter,(Titlecase means that the fist letter of each word is capitalized) 14, Write a program that takes a sentence as an input parameter where each word in the sentence is separated by a space. The function should replace each blank with a hyphen and then return the modified sentence. 15. Write a script to partition the st 16. What will be the output of the following programming code? ring ‘INSTITUTE’ at the occurrence of letter str="My Python Programming" print (str ({-5:-1]) print (str[1:5]) print (str[:-4]) print (str[0:]) Print (str[:13-4]) Print (str[:3]) 17. Write a program to count the number of each vowel in 2 given S108: 18. Write a program that reads @ line, then counts how many times the word ‘is’ appears in the line and displays the count. 18. Write a program to remove “if any) from a string 4) CASE-BASED/SOURCE-BASED INTEGRATED QUESTIONS 2, Ans. 1, ABC Dialling Networks Pvt. Ltd. is a company that deals in maintaining the Phone customers globally. It prefixes the area code automatically at the time of adding the phone its database. is ‘Write a program that prompts for a phone number of 10 digits and two dashes, a dash each ap code and the next three numbers, For example, 017-555-1212 is a legal input. Display if ¢y number entered is a valid format or not and if the phone number is valid and not (i.e., contai digits and dashes at specific places). Jp sinput (renter Phone Nesber fal = False 12 and pis) == pit : sptaeT) spl8z)) ssdight 0 fall the characters except dashes should be aigits “print (pyis valid) ‘eine (py"is Snvalld") > RESTART: C:/Users/preeti/AppData/Local/ casestudyl.py Enter Phone Number :223098088 223098888 is invalid >>> RESTART: C1/Users/preeti/AppData/Local/ casestudy1.py Enter Phone Number 989-234-3377 989-234-3377 is valid . Kids Elementary Technologies help nursery children to improve their handwriting and writing skills such as word formation, recognition of lower and uppercase letters and small sentences formation, Write a program that should prompt the child to type some sentence(s) followed by “enter”. It should then print the original sentence(s) and the following statistics relating to the sentence(s): * Number of words ‘+ Number of characters (including white-space and punctuation) * Percentage of characters that are alphanumeric nunber_of_characters = 1en(s) jal_nun = 0 for i in sz if iisalnun0: ‘al_num += 1 ‘'z @ there is a space means there is another vord ____ Runber_of_vords += 1 print ("number Of Words are”, number_of_vords) jprint ("nunber_of_characters are” number_of_characters) [print ("percentage of characters that are alphanumric {s",al_num*100/1en(s),"8") ind 3 RESTART! C:/Users/preeti /appData/Local /Prograns/Python/Py! Je_paraneters.py enter a sentence : This Utility is for Nursery KIDS Inunber of words are 6 nunber_of_characters are 32 percentage of characters that are alphanumric is 64.375 +