About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .
Python Is an easy to learn 、 Powerful programming language . It provides an efficient high-level data structure , It's also a simple and effective way of object-oriented programming .Python Elegant grammar and dynamic typing and the essence of interpretive language , Make it an ideal language for scripting and rapid application development on most platforms . Now let's introduce python Realize the text progress bar through string related knowledge .
example : Programming , Analog output simple non refresh text progress bar . It is required to divide the whole task into 100 A unit of , Every execution 10% Output a progress bar , Each line of output contains a percentage of progress 、 Represents the completed part (**) And unfinished parts (..) Two characters of , And a small arrow that follows the degree of completion , The style is as follows :
%10 [**->………………]
The code is as follows , Each line is explained in the notes .
import time # Import time modular
scale = 10 # Variable scale Used to indicate the accuracy of the output progress bar
print("---------- Execution start ----------") # Output
for i in range(scale + 1): # Loop variable from 0 To 10
a = "**" * i # use “*” Indicates the completed part
b = ".." * (scale - i) # use “.” Indicates the unfinished part
c = (i / scale) * 100 # Calculate the completion percentage and assign it to c
print("%{:^3.0f}[{}->{}]".format(c, a, b)) # Format output
time.sleep(0.1) # Pause 0.1 second
print("---------- end of execution ----------") # Output
adopt pycharm The results are as follows .
Insert picture description here
1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial
That's about Python Realize the text progress bar through string related knowledge ., You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .