Python exercise 018: Print asterisks and python018

Source: Internet
Author: User

Python exercise 018: Print asterisks and python018

[Python exercise 018]Print the example (diamond ):

   *  *** ************ *****  ***   *

----------------------------------------------

This is simple. You only need to know the built-in method str of Python. center (width [, fillchar]) can be easily printed: str is an asterisk with varying numbers, width is the maximum width (7 spaces), and the default fill character fillchar is a space. Two for loops! The Code is as follows:

s = '*'for i in range(1, 8, 2):    print((s*i).center(7))for i in reversed(range(1, 6, 2)):    print((s*i).center(7))

For the output result, see the question.

 

[Updated on ]--------------------------------------------

Of course, it is not difficult to solve the str. center () problem. It is simply to calculate the number of spaces. The Code is as follows:

s = '*'for i in range(1,8,2):    t = (7-i)//2    print(' '*t + s*i + ' '*t)for i in reversed(range(1,6,2)):    t = (7-i)//2    print(' '*t + s*i + ' '*t)

 

++

Source: getting started with programming languages: 100 typical examples [Python]

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.