Difference between in ""," " in python - python

what is the difference between "" and " " because
for i in range(6):
for j in range(6-i):
print(" ",end="")
for j in range(i):
print(i,"",end="")
print()
gives
and
for i in range(6):
for j in range(6-i):
print(" ",end="")
for j in range(i):
print(i," ",end="")
print()
gives
and
for i in range(6):
for j in range(6-i):
print(" ",end="")
for j in range(i):
print(i,end="")
print()
gives
I wanted to know the difference "" and " " because "" also gives some space

the behavior from your 3 examples it is given by the built-in function print, the default sep=' ' as you can see in the docs, this means that even if you have an empty string "" in 3 consecutive items you will have 2 spaces in the output when you print them, having the one in the middle the empty string will give you the impression (at the output) that there are 2 spaces between 2 consecutive items; printing 3 elements with the one in the middle as the space character " " will give your the impression that between 2 consecutive elements there are 3 spaces
print(i, "", 1)
output:
5 1 # 5+sep_1space+the_empthy_string+sep_1space+1
print(i," ", 1)
output:
5 1 # 5+sep_1space+1space+sep_1space+1
print(i,1)
output:
5 1 # 5+sep_1space+1

Related

Why my code does not produce the pattern in the 2nd loop?

Why does my code not work in the 2nd iteration? I want to give input in row = 3 or 5 or x, and then I expect it to produce the following output pattern:
#When row = 3
1|. . #
2|. ##
3|###
Please note that the number of "." will be opposite to the number of "#"
The i loop prints a new line, the j loop prints "#", and the k loop prints "."
row = 3
for i in range(1, row + 1):
for j in range(1, i + 1):
for k in range(1, (row - i) + 1):
print(".", end= "")
print("#", end="")
print("")
Your k loop runs too many times to do what you want after the first iteration.
However I think this does what you want, hope it helps:
row = 3
n_hash = 0
n_dots = row
for i in range(row):
print(f'{i+1}|{n_dots * "."}{n_hash * "#"}')
n_dots -= 1
n_hash += 1

how to print star hypen pattern in python

How to print the pattern like this:
(need to print n-1 lines)
input=3
----#
--#-#-#
input=6
----------#
--------#-#-#
------#---#---#
----#-----#-----#
--#-------#-------#
My code:
row = int(input())
for i in range(1, row):
for j in range(1,row-i+1):
print("-", end="")
for j in range(1, 2*i):
if j==1 or j==2*i-1:
print("#", end="")
else:
print("-", end="")
print()
MY OUTPUT:
input=5
----#
---#-#
--#---#
-#-----#
Please explain how to do??
There are a few things missing and to be improved in your code:
There's no need to make a loop to print the same character again and again: on python you can use the product to repeat the character an x number of times. For example: "-" * 3 == "---"
They way you calculate the hyphens in the middle is fine, but you need to do it twice and add an "#" in between.
You can build the strings part by part first and then print the whole line, avoiding having to print an empty line in the end of the loop.
Personally, since the first line is going to have one "#" and not three, I prefer to calculate it and print it separately.
With these improvements, a solution to your problem could be:
row = int(input())
print("-" * (row - 1) * 2 + "#")
for i in range(row - 2, 0, -1):
left_hyphens = "-" * i * 2
mid_hyphens = "-" * (1 + 2 * (row - 2 - i))
print(left_hyphens + "#" + mid_hyphens + "#" + mid_hyphens + "#")
row = int(input())
for i in range(1, row):
for j in range(1,2*(row-i)+1):
print("-", end="")
for j in range(1, 4*i):
if j==1 or j==2*i-1 or j==4*i-3:
print("#", end="")
elif j<=4*i-3:
print("-", end="")
print()

Alternating lines with Nested Loops

This is the exercise I need to complete:
Using nested loops, write some code that outputs the following:
##########
**********
##########
**********
##########
Following is all the code I have so far. I assume I need to combine these two loop functions to one but I'm struggling with it. I'm early on in my first class and any help with this would be appreciated!
for a in range (0, 5, 2):
for b in range(10):
print("#", end = "")
print("")
for a in range (1, 5, 2):
for b in range(10):
print("*", end = "")
print("")
Since no input is specified, only a fixed output:
for _ in '_':
for _ in '_':
print('''##########
**********
##########
**********
##########''')
And yes, if that was my homework exercise, I'd absolutely submit this.
There are several ways to do it. First you should check how many lines you need to output. 5, so you need a loop doing something 5 times.
for i in range(5):
Now you need 2 loops to paste print the 2 line patterns (you already have them in your code)
for b in range(10):
print("#", end = "")
print("")
and
for b in range(10):
print("*", end = "")
print("")
If you need to alternate between 2 values it's mostly the best to use the Modulo Operator.
So you can just switch between the 2 loops by % 2.
for i in range(5):
if i % 2 == 0:
for b in range(10):
print("#", end = "")
print("")
else:
for b in range(10):
print("*", end = "")
print("")
I would suggest to simply base the symbol on the row index, even or odd
nb_lines = 5
line_size = 10
for i in range(nb_lines):
for _ in range(line_size):
if i % 2 == 0:
print("#", end="")
else:
print("*", end="")
print()
But no need of nested loops
nb_lines = 5
line_size = 10
for i in range(nb_lines):
if i % 2 == 0:
print("#" * line_size)
else:
print("*" * line_size)
def printStripes(row_length, number_of_rows):
for _ in '_':
print( "".join(
[ "\n" if n%(row_length+1)==row_length else
"#" if (n//(row_length+1))%2==0 else
"*"
for n in range(number_of_rows*(row_length+1))
]
),end="")
printStripes(row_length=5,number_of_rows=8)
But don't tell your teacher that you got if from stackoverflow. (Thanks Kelly, for how to deal with the nested loop constraint.)
You can use this to get the result that you want
for i in range(0,5):
print("*"*10)
print("#"*10)

Print 2d list as a game board

I have a 2d list [1,2,3,4],[4,3,3,1],[3,2,1,1],[2,2,2,1] and I want to print it out to match the following format.
0123
XXXXXXXX
0*1234*0
1*4331*1
2*3211*2
3*2221*3
XXXXXXXX
0123
It should not be hard coded and the length of the list = n so this list n=4 but if list n=5 there would be 5 digits per row and the number on the sides would go 0,1,2,3,4.
So far all I have is:
for row in board:
for column in row:
print(column, end="")
print("")
Which only outputs the list as:
1234
4331
3211
2221
please help me add all the special stuff.
so I find a solution for what you want to do however I think it can be improved a lot (not program quite a lot in python), but I'll leave it here anyways :)
print(" ", end="")
for x in range(len(board)):
print(x, end="")
print()
for x in range(len(board)+4):
print("X", end="")
print()
for num,row in enumerate(board):
print(f"{num}*", end="")
for column in row:
print(column, end="")
print(f"*{num}", end="")
print("")
for x in range(len(board)+4):
print("X", end="")
print()
print(" ", end="")
for x in range(len(board)):
print(x, end="")
print()
Well for the first line you pretty much print 2 spaces, followed by all the digits between 0 and the number of columns in your board. You can use the "range" function for this. Then you must print the correct amount of 'X'. The correct amount is number of columns + 4, I think you can see why.
You should keep a counter starting from 0. For each row you print, you must print the string value of the counter, followed by an asterisk(*), followed by your row, followed by an asterisk(*) and finally followed by the same counter value. You must increment the counter by one for each row.
The last 2 rows are the same as top 2.
I do not want to share my code because this is such a simple problem, I think solving it on your own will help you in the long run.
Probably an unpopular opinion, but I think these kinds of problems are really fun.
This solution formats the contents correctly, even if you change the number of rows, and even the number of items in each row.
def draw_board(board):
# determining the number of elements in a row,
# this is used for printing the X's
# and printing the spaced ranges (i.e. " 0123 ")
n = len(board[0])
# calculating the total width of the board,
# + 4 is because of the "0*...*0" situation
width = n + 4
# calculating margin of the spaced ranges
# (i.e. calculating how much space on each side)
margin = int(n / 2)
# printing the spaced ranges using the margin
print(" " * margin + "".join(str(num) for num in list(range(n))) + " " * margin)
# printing the XXXX
print("X" * width)
# printing the row index number,
# with the *,
# along with the numbers in the row,
# followed by the * and the row number
for row in range(len(board)):
print(str(row) + "*" + "".join(str(elem) for elem in board[row]) + "*" + str(row))
# printing the XXXX
print("X" * width)
# printing the spaced ranges using the margin
print(" " * margin + "".join(str(num) for num in list(range(n))) + " " * margin)
b = [
[1,2,3,4],
[4,3,3,1],
[3,2,1,1],
[2,2,2,1]
]
draw_board(b)
# OUTPUT:
# 0123
# XXXXXXXX
# 0*1234*0
# 1*4331*1
# 2*3211*2
# 3*2221*3
# XXXXXXXX
# 0123
Edited to remove my own tests and to reflect given problem.

Pyramid of letters program in Python

Have a program request the user to enter a letter. Use nested loops to produce a pyramid pattern like this:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
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.
L=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
letter=raw_input('Enter letter:')
g=letter.upper()
if g in L:
f=L.index(g)
a=f+1
for k in range(a,0,-1):
b=L[:a-k+1]
d=L[:a-k]
L3=[x for x in d]
e=L3[::-1]
print ' '*k + '%s'%''.join(b) + '%s'%''.join(e)
for k in range(a):
d=L[:a-1-k]
b=L[:a-k-1]
L3=[x for x in b]
e=L3[::-1]
print ' '*(k+2) + '%s'%''.join(d) + '%s'%''.join(e)
what I'm getting is something like this
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDDCBA
ABCCBA
ABBA
AA
There might be different ways to do this efficiently, but here is a simple, easy to understand solution (basic loops)
letter = raw_input('Enter Input: ')
if (len(letter) == 1):
if (letter.isalpha()):
# Retrieve the positon of the input letter
pos = ord(letter.upper()) - 64
# Prints upper part of the diamond
for i in range(1, pos + 1):
# Prints leading spaces for upper pyramid
for j in range(pos - i, 0, -1):
print(" "),
# Print numbers
# 2 for loops to print ascending and descending letters in a single row
for j in range(0, i):
print(chr(65+j)),
for j in range(i-1 , 0, -1):
print(chr(64+j)),
print
# Prints lower part of the diamond, This is just the reverse of the upper one
for i in range(pos -1 , 0, -1):
# Print leading space for lower pyramid
for j in range(pos - i, 0, -1):
print(" "),
for j in range(0, i):
print(chr(65+j)),
for j in range(i-1 , 0, -1):
print(chr(64+j)),
print
else:
print 'input a letter from the alphabet only'
else:
print 'enter one letter only'
Do half the pyramid, then reverse the array and print without the last (now first) element.

Categories