Printing a square pattern in Python with one print statement - python

I want to print a square pattern with "#", the output should look something like this:
# # # # #
# #
# #
# #
# # # # #
The code I was able to write is this:
n=10
for i in range (1,6):
for j in range(1,6):
if i ==1 or 1==n or j==1 or j==n:
print("#", end= ' ')
else:
print(" ", end="")
print()
The output that came is this:
# # # # #
#
#
#
#
I also would like to know if it's possible to only have one print statement instead of many.
Thank you.

This works!
s=5
print(" ".join("#"*s))
for i in range(0,s-2):
print("#"+" "*(s+2)+"#")
print(" ".join("#"*s))
>>>
# # # # #
# #
# #
# #
# # # # #
Single line:
print(" ".join("#"*s)+"\n"+("#"+" "*(s+2)+"#"+"\n")*(s-2)+" ".join("#"*s))
>>>
# # # # #
# #
# #
# #
# # # # #

A simple one-liner for this with a whole square filled in could be
size = 6
print('\n'.join([' '.join('#' * size)] * size))
Or if you want the same pattern in the original
print(' '.join('#' * size))
print('\n'.join('#' * (size - 1)))

I noticed the empty spaces in between the hashtags, use this function it should give you custom dimensions too
def createSquare(xLen,yLen):
for i in range(1,yLen):
if i == 1 or i == yLen-1:
print("# "*xLen)
else:
print("#"+" "*int(xLen*2-3)+"#")
createSquare(10,10)

Related

Program, that creates triangle and square

I've started learning programming and I need to create program, where user can enter amount of rows wanted and then program has to print two different shapes according to the info given by user. Shapes have to be like
Blockquote
# # # # # *
# # * *
# # AND * * *
# # * * * *
# # # # # * * * * *
I managed to do the triangle, but I can't figure out, how to create square that is empty inside. I have only done it filled inside.
Can anyone help me to modify my code?
userInput = input("Enter amount of row's wanted: ")
def shape(userInput, drawCharacter):
n = 0
while n < int(userInput):
n += 1
if drawCharacter == "*":
print(n*drawCharacter.rjust(3))
elif drawCharacter == "#":
print(int(userInput)*drawCharacter.rjust(3))
shape(userInput, "*")
print("|__________________|\n")
shape(userInput, "#")
A method using numpy array to avoid loops when generating the matrix:
import numpy
n = 5 # or userinput, has to be >= 2
mat = np.full((n,n), '#') # a matrix of #s
mat[1:-1, 1:-1] = np.full((n-2, n-2), ' ') # make the center of the matrix ' '
print('\n'.join([' '.join(e) for e in mat]))
result:
# # # # #
# #
# #
# #
# # # # #
Your box consists of basically following parts:
Top and bottom rows: print (width * '#')
And center rows: print ('#{}#'.format(' ' * (width - 2)))
And as an exercise you just need to figure out the loop.. ;)
If this is your first encounter with programming(any language), what I would recommend you to do is to try to implement this problem with nested for loops (which will simulate a 2d-array, or basically a matrix), try to figure out what index-es of the matrix not to print out and print out only the edges.
By doing this approach you will get a much better in-depth understanding of the problem that this task presents and how to solve it. Good luck!

How do you make this shape in Python with nested loops?

Write a program using nested loops to draw this pattern. (14) bottom page
for row in range(6): # this outer loop computes each of the 6 lines
line_to_print = '#'
for num_spaces in range(row): # this inner loop adds the desired number or spaces to the line
line_to_print = line_to_print + ' '
line_to_print = line_to_print + '#'
print line_to_print
prints this output:
##
# #
# #
# #
# #
# #

Replacing list element within list element with a string

So I am trying to create a grid that can have it's individual "grid squares" replaced by any given symbol. The grid works fine but it is made of lists within lists.
Here's the code
size = 49
feild = []
for i in range(size):
feild.append([])
for i in range(size):
feild[i].append("#")
feild[4][4] = "#" #This is one of the methods of replacing that I have tried
for i in range(size):
p_feild = str(feild)
p_feild2 = p_feild.replace("[", "")
p_feild3 = p_feild2.replace("]", "")
p_feild4 = p_feild3.replace(",", "")
p_feild5 = p_feild4.replace("'", "")
print(p_feild5)
As you can see that is one way that I have tried to replace the elements, I have also tried:
feild[4[4]] = "#"
and
feild[4] = "#"
The first one replaces all "#" 4 elements in from the left with "#"
The second one gives the following error
TypeError: 'int' object is not subscriptable
The make a grid of # with row 3, column 3 replaced with #:
>>> size = 5
>>> c = '#'
>>> g = [size*[c] for i in range(size)]
>>> g[3][3] = '#'
>>> print('\n'.join(' '.join(row) for row in g))
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
May be you're looking for this :-
size = 49
feild = []
for i in range(size):
feild.append([])
for i in range(size):
map(feild[i].append, ["#" for _ in xrange(size)])
i = 4
feild[i][0] = "#"

python nested loop print spaces between two characters every repeat

this is the question i am trying to solve
i have tried everything to get the spaces to appear between the hashtags but have failed. i don't know what else to do
this is what i have done so far, i found a few ways to get only 1 space between the hashtags, but to have them repeat every time is what i have not been able to do
star = 6
for r in range(star):
for c in range(r - 5):
print ' ',
print '##',
print
this is the output i get
any help is appreciated.
def hashes(n):
for i in range(n):
print '#' + ' '*i + '#'
Testing
>>> hashes(1)
##
>>> hashes(4)
##
# #
# #
# #
Obviously, there are more succinct ways of doing this, but the original question called for nested loops:
import sys
inner = 1
for x in range(6):
sys.stdout.write('#')
for y in range(inner):
if y == inner - 1:
sys.stdout.write('#')
else:
sys.stdout.write(' ')
sys.stdout.write('\n')
inner += 1
Output:
$ python loops.py
##
# #
# #
# #
# #
# #

How to create patterns in Python using nested loops?

I am trying to create this pattern in Python:
##
# #
# #
# #
# #
# #
I have to use a nested loop, and this is my program so far:
steps=6
for r in range(steps):
for c in range(r):
print(' ', end='')
print('#')
The problem is the first column doesn't show up, so this is what is displayed when I run it:
#
#
#
#
#
#
This is the modified program:
steps=6
for r in range(steps):
print('#')
for c in range(r):
print(' ', end='')
print('#')
but the result is:
#
#
#
#
#
#
#
#
#
#
#
#
How do I get them on the same row?
Replace this...:
steps=6
for r in range(steps):
for c in range(r):
print(' ', end='')
print('#')
With this:
steps=6
for r in range(steps):
print('#', end='')
for c in range(r):
print(' ', end='')
print('#')
Which outputs:
##
# #
# #
# #
# #
# #
It's just a simple mistake in the program logic.
However, it is still better to do this:
steps=6
for r in range(steps):
print('#' + (' ' * r) + '#')
To avoid complications like this happening when using nested for loops, you can just use operators on the strings.
Try this simpler method:
steps=6
for r in range(steps):
print '#' + ' ' * r + '#'
You forgot the second print "#". Put it before the inner loop.
Try something like this:
rows=int(input("Number"))
s=rows//2
for r in range(rows):
print("#",end="")
print()
for r in range(rows):
while s>=0:
print("#"+" "*(s)+"#")
s=s-1
print("#")

Categories

Resources