Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

33 python format exercises using format method

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report