Yahoo India Web Search

Search results

  1. Method #1: Comprehension version. This could be easiest way to print any pattern in Python. n = 5. print('\n'.join([('* '*i).center(2*n) for i in range(1,n+1)])) Use center method of str, give it a width as parameter and it will centered the whole string accordingly. Method #2: Regular version. n = 5.

  2. May 16, 2017 · I've written a program in C++ that displays a pyramid of asterisk (see below) and now I'd like to see how it's done in Python but it's not as easy as I'd thought it would be.

  3. Nov 18, 2013 · I'm trying to use a recursive function to print an upside-down pyramid in Python while also using indentation. What I have so far is: def printStars(n, indent): if n == 0: return elif n ...

  4. Mar 18, 2016 · All you need to do is add another two loops that: 1) Replicate the format () template's white space padding. 2) Create the 'true' centered string by putting a space between stars. This could be done with three while loops, but I don't see how that's pythonic.

  5. Oct 14, 2018 · I have begun programming with Python and I made this simple program drawing stars in the shape of the ...

  6. May 3, 2020 · # Function to demonstrate printing pattern triangle def triangle(n): # number of spaces k = 2*n - 2 # outer loop to handle number of rows for i in range(0, n): # inner loop to handle number spaces # values changing acc. to requirement for j in range(0, k): print(end=" ") # decrementing k after each loop k = k - 1 # inner loop to handle number of columns # values changing acc. to outer loop for j in range(0, i+1): # printing stars print("* ", end="") # ending line after each row print("\r ...

  7. Oct 2, 2013 · Remmeber until now, we've still just been working with the left half of the pyramid. So we take the output array, reverse it ( array[::-1] ) and then take every element but the first ( array[1:] ) and join it all together with a string and print it out.

  8. Jul 6, 2016 · Looking your code, there were only 2 issues: 1. You were printing in increasing order. So if you subtract the outer from inner, it would print the numbers in decreasing order 2. The padding varies from one line to the next, so add the padding in the outer loop. This is how your code would look with the 2 revisions: outer = 1. while outer <=6:

  9. Feb 22, 2015 · The pattern should extend to the character entered. For example, the preceding pattern would result from an input value of E or e. Here's what I've done so far and it almost gives the pattern with minor defects in the lower right hand part of the diamond.

  10. Dec 2, 2017 · 1. You can create one line of n times the * symbol by using join on something similar to a list comprehension, namely a generator expression: >>> ''.join('*' for _ in range(5)) '*****'. You can create output consisting of multiple lines by joining the individual lines with the newline character '\n' (as opposed to the empty string '' like before):

  1. People also search for