6.(Strings in Python)
6.(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:]) (vil) print (mysubject.. swapcase()) (i) 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) Mpethet 2 exists In sting ‘Schoo!’ of not. : the string str1="Global Warming”, ents in Python to implement the following: wet ply the ast four characters e a To replace all the occurrences of letter ‘ain the sting with ith “00 output of the following program code: . tyirus-virus-virus! 4 following: ( idet consi 9, co tte yo write th ae . ur ["Fundamentals", "of", "Python" =m" #no space ai print (d1-Jo!n ee | gy, What will be the output of the following code? qext = "Mind@Work!" ja = len(Text) aText = "" gor 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) tn input the string ‘My Schoo. Write a script to partition the string atthe occurrence of letter W. 13. Wite a program to convert a string with more than one word into titlecase string where sting 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 S178: 1. 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 4CASE-BASED/SOURCE-BASED INTEGRATED QUESTIONS 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. , 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 @ legal input. Display if number entered format or not and if the phone number is valid and not (i, nt a eae 2d ota) on sos and pit oe te Sa at arian cep eeaae saa 5 | co >>> ‘J RESTART: C:/Users/preeti /AppData/Local/ Enter Phone Number :223098888 casestudyl.py 223098888 is invalid >>> RESTART: C1/Users/preeti/AppData/Local/ casestudy1.py Enter Phone Number :989-234-3377 989-234-3377 is valid 2. 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 Ans. unber_of_characters = len(s) jal_nun = 0 for i in sz if iisalnun0: ‘al_num += 1 ‘ "2 @ there is a space means there is another vord ____ Rusber_of_vords += 1 print ("nunber 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 number of words are 6 nunber_of_characters are 32 percentage of characters that are alphanumric is 04.375 ¢