Like other programming languages, you can compile some small programs to familiarize yourself with and master the syntax when learning python. I accidentally discovered the special use of the asterisk (*) on strings when writing a small program to learn for statements. The following is an example. Write a piece of code output in the form.
*
**
***
****
*****
******
*******
********
*********
**********
***********
In VB or Delphi, if we want to repeat a string for n times, we generally write a for loop to implement it. Therefore, in the code that prints the triangle above, I used the most basic method.
For I in range (1, 12): S = "" For J in range (1, I): S + = "*" Print s
Can I use only one for statement to achieve the same output result? It is easy to implement in Python.
For I in range (1, 12): Print "*" * I
You only need to perform the asterisk (*) operation on the string to be output. If the left and right sides of an asterisk are numbers, it indicates multiplication. If one side of an asterisk is a string and the other side is a number, it indicates the number of times that the string is repeated.
After learning about this, the above Code is easy to understand and output I asterisks on line I.
Print 5 * "AB" # The output result is abababababprint "Python" * 2 # The output result is pythonpython.