Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

33 python format exercises using format method

Shulou Source: shulou.com Published: 2022-06-03 08:16:15 09月21日 Update

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 ("{:

Tags: Input characters strings statistics methods variables console results controls exercises loops triangles iterations asterisks formats times programs triangles loops generation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Xiaomi Redmi macOS MySQL