Application of Python random Asterisk on strings

Source: Internet
Author: User

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.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.