33 python format exercises using format method
Lesson 15 exercise 1. Write a Python program that inputs a string from the console (saved to variable s), then continuously inputs string (saved to variable subStr) through the while loop, and counts the number of times subStr appears in s, and finally uses the format method to format the statistical results. S = input ("Please enter a string:") while True: subStr = input ("Please enter the string to be counted:") if subStr = = ": exit": break; I = 0 count = 0 while I
< len(s): index = s.find(subStr, i) if index >-1: count + = 1 I = index + len (subStr) else: break The output of print ("{} 'appears {} times" .format (subStr, s, count) in' {}') is: please enter a string: l love python Please enter the string to be counted: ointo' appears twice in'l love python'--''2. Enter n from the console and use the format method to generate an asterisk triangle * 'floorStr = input (' Please enter a number of layers:') floor = int (floorStr) num = floor * 2-3 # 17while floor > 0: print ("{: