Python String Exercises Let’s check out some exercises that will help you understand Python strings better. Exercise 9-a Assign the string below to the variable in the exercise. "It's always darkest before dawn." #Type your answer here. str="" print(str) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(str,"It's always darkest before dawn.","str checks") myTests().main() Hint 1 All you need to do is make sure your string is inside quotation marks. Solution str = "It's always darkest before dawn." Exercise 9-b By using first, second and last characters of the string, create a new string. str="It's always darkest before dawn." #Type your answer here. ans_1= print(ans_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,str[:2]+str[-1],"str checks") myTests().main() Hint 1 You can add strings by using (+) character. Solution ans_1 = str[0]+str[1]+str[-1] Exercise 9-c Replace the (.) with (!) str="It's always darkest before dawn." #Type your code here. print(str) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(str,"It's always darkest before dawn!","str checks") myTests().main() Hint 1 You can’t directly change a string, because they’re immutable. But you can use .replace() method and assign a new string to the same variable. Hint 2 .replace() will take two arguments, first: value to replace, second: new value. Solution str = str.replace(".", "!") Exercise 9-d Reassign str so that, all its characters are lowercase. str="EVERY Strike Brings Me Closer to the Next Home run." # Type your code here. print(str) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(str,str.lower(),"str checks") myTests().main() Hint 1 .lower() method will create a version of your string with all lowercase characters. Solution str = str.lower() Exercise 9-e Now make everything uppercase. str="don't stop me now." # Type your code here. print(str) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(str,str.upper(),"str checks") myTests().main() Hint 1 .upper() method will create a version of your string with all uppercase characters. Solution str = str.upper() Exercise 9-f Make the string so that everything is properly and first letter is capital with one function. str="there are no traffic JamS Along The extra mile." #Type your answer here. print(str) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(str,str.capitalize(),"str checks") myTests().main() Hint 1 .capitalize() will make the first letter uppercase and everything else lowercase. Solution str = str.capitalize() Exercise 9-g Does the string start with an A? Assign a boolean answer to the ans_1 variable. str="There are no traffic jams along the extra mile." # Type your code here. ans_1= print(ans_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,str.startswith("A"),"ans_1 checks") myTests().main() Hint 1 .startswith() method checks if the first letter of the string is a certain character or not. It returns True or False. Solution ans_1 = str.startswith("A") Exercise 9-h Does it end with a fullstop (.) ? str="There are no traffic jams along the extra mile." # Type your code here. ans_1= print(ans_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,str.endswith("."),"ans_1 checks") myTests().main() Hint 1 .endswith() will check the last character of a string. Solution ans_1 = str.endswith(".") Exercise 9-i Using .index() method, identify the index of character: (v). str="The best revenge is massive success." # Type your code here. ans_1= print(ans_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,str.index("v"),"ans_1 checks") myTests().main() Hint 1 .index() method helps you identify the index of a substring. Solution ans_1 = str.index("v") Exercise 9-j Using .find() method, identify the index of character: (m). str="The best revenge is massive success." # Type your code here. ans_1= print(ans_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,str.find("m"),"ans_1 checks") myTests().main() Hint 1 .find() method also helps you identify the index of a substring. Solution ans_1 = str.find("m") Exercise 9-k Try to see what results you get looking for character: (X). First with .find() method and then with .index() method. str="The best revenge is massive success." # Type your code here. ans_1= print(ans_1) Hint 1 .find() method returns -1, when it can’t find a character while .index() method returns a ValueError. Solution ans_1 = str.find("X") Exercise 9-l Which character occur more often in the string? "a" or "o" ? Print both counts inside the print function. str="People often say that motivation doesn't last. Well, neither does bathing. That's why we recommend it daily." # Type your code here. ans_1= ans_2= print("count of a is: ", ans_1, " count of o is: ", ans_2) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,str.count("a"),"ans_1 checks") self.assertEqual(ans_2,str.count("o"),"ans_2 checks") myTests().main() Hint 1 .count() method tells how many times character(s) occur in a string. You need to type the value you’re checking inside .count() Solution ans_1 = str.count("a")ans_2 = str.count("o") Exercise 9-m Print the types of two given variables with the print function. v_1="1" v_2=1 # Type your code on line 4: ans_1= ans_2= print(ans_1) print(ans_2) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,type(v_1),"ans_1 checks") self.assertEqual(ans_2,type(v_2),"ans_2 checks") myTests().main() Hint 1 You can use type() function with strings and many other types of data. Solution ans_1=type(v_1) ans_2=type(v_2) Exercise 9-n What is the length of the given string? str="1.975.000" # Type your code here: ans_1= print(ans_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,len(str),"string length checks") myTests().main() Hint 1 len() function will give you the character length of your string. Solution ans_1 = len(str)