How can I remove the space? - python

I have a program which transposes a matrix. My question is how to remove the space after every last number in row.
I know that the space is there because of the end = " ". The end makes a space after every number but how can I remove it from the last number in rows? How can I replace the end?
p = input ()
M = str(p).split(" ")
A = int(M[0])
R = int(M[1])
X=[]
for i in range(A):
l=list(map(int,input().split(" ")))
X.append(l)
for i in range(R):
for j in range(A):
print(X[j][i], end = " ")
print ()

It sounds like you only want spaces in between each number. The best way to do this is the join method, which operates on a delimiter, takes a list of strings, and creates a new string where the delimiter separates each string in the list.
For example,
>>> " ".join(["1", "2", "3"])
'1 2 3'
You can use this in your code. Here is the final version:
p = input()
M = str(p).split(" ")
A = int(M[0])
R = int(M[1])
X = []
for i in range(A):
l = list(map(int, input().split(" ")))
X.append(l)
for i in range(R):
row = [str(X[j][i]) for j in range(A)]
print(" ".join(row))

Related

How to remove trailing spaces using rstrip from a string made from a loop

I want to write a program that prints a word 3 times. The first two iterations should have a space after them, but the 3rd line shouldn't.
Example of desired output: https://imgur.com/43pYOI9
This is the code I'm trying:
n = input("Enter a word: ")
for x in range(len(n)):
print((n[x]+" ")*3)
n.rstrip()
This is the output I'm getting - rstrip() doesn't seem to be working.
https://imgur.com/a/NM1uEQI
Any suggestions on how to fix this?
You need to rstrip() before you print and you need to rstrip() what you are actually printing, not the whole line. This would work
n = input("Enter a word: ")
for x in range(len(n)):
s = (n[x] + " ") * 3
print(s.rstrip())
A better way would be to use join()
n = input("Enter a word: ")
for x in range(len(n)):
print(" ".join(n[x] * 3))
And you can iterate over string directly, so this would also work
n = input("Enter a word: ")
for x in n:
print(" ".join(x * 3))
Or you could get clever and do something like this
n = input("Enter a word: ")
list(map(print, [" ".join(i * 3) for i in n]))
You almost had it. You're calling rstrip() after the print line which has no impact since it's already been printed.
n = input("Enter a word: ")
for x in range(len(n)):
print((n[x]+" ")*2 + n[x])

How to print outputs of different loops in one line, separated by a space?

I want to print the output of these two loops in the same line, separated by a space.
number = int(input())
words = []
for y in range(0, number, 1):
word = str(input())
words.append(word)
for i in range(0, len(word), 2):
print(word[i], end='')
for t in range(1, len(word), 2):
print(word[t], end='')
If I enter the word 'code', I get cdoe, when I actually want cd oe.

Removing comma and quotations when printing list

When I am printing the list, there is a comma and quotations in the letter X, how do I remove it?
#asks user input
m = int(input("Enter number of boxes horizontally and vertically: "))
n = int(input("Enter number of mines: "))
a=[]
for i in range(m):
a.append([])
for k in range(m):
a[i].append("X")
i=1
#prints the generated cells
for i in range(m):
print a[i]
i=i+1
You are looking to use join to make your list in to a string. You want to make your string space separated, so you will want to use ' '.join():
Change this:
print a[i]
to this:
print(' '.join(a[i]))
Or, if you are mixing types, you should do:
' '.join(str(x) for x in a)
you can also use this:
print ' '.join(map(str, a[i]))

python asterisk triangle without using y* "*"

I'm trying to create a iso triangle (one that starts in the middle).
I have a code but the problem is that I'm not allowed to use Y* "*" 5 in my code.
(The y is a variable there)
Also I may only use one print statement at the end of my code.
Can you please help me out.
f = int(raw_input("enter"))
for i in range(f):
print " " * (f-i-1) + "*" * (2*i+1)
creats this triangle
*
***
*****
*******
*********
***********
However, you are not allowed to use the *-operator on string and int. So for example ''***'' * 3 is not allowed, but 3 * 4 is
This just creates a continuous string and then prints it at the end
f = int(raw_input("Enter height: "))
s = ''
for i in xrange(f):
for j in xrange(f-i-1):
s += ' '
for j in xrange(2*i+1):
s += '*'
s += '\n'
print s
This is a solution which i think is very easy to understand. You can make the parameter of range() variable, to make it more dynamic.
from __future__ import print_function
for i in range(1,12,2):
count=(11-i)/2
for j in xrange(count):
print(" ",end='')
for j in xrange(i):
print("*",end='')
for j in xrange(count):
print(" ",end='')
print(end="\n")
I think the best solution is using the center() string method:
f = int(raw_input("How many rows to print in the triangle? "))
star = "*"
full_string = ""
for X in xrange(f):
star += "**" if X>0 else ""
full_string += star.center(2*f-1) + "\n"
print full_string[:-1]
The str.center() documentation:
https://docs.python.org/2/library/string.html#string.center
EDIT: If you can't use the print statement within the for loop, you could concatenate the string during the loop and print it at the end:
f = int(raw_input("How many rows to print in the triangle? "))
star = "*"
full_string = ""
for X in xrange(f):
# the first row should take only one star
star += "**" if X>0 else ""
star2 = star.center(2*f-1)
full_string += star2 + "\n"
# slice the string to delete the last "\n"
print full_string[:-1]
I noticed that using a for loop add a newline character. If you want to avoid this, you can slice the string before printing.
There is no problem with this code, i just checked it and it worked fine. If you would post the error message we might be able to help a bit more.

Write a function that takes as a parameter a list of strings and returns a list containing the lengths of each of the strings

WHOLE QUESTION: Write a function that takes as a parameter a list of strings and returns a list containing the lengths of each of the strings. That is, if the input parameter is ["apple pie", "brownies","chocolate","dulce de leche","eclairs"], your function should return [9, 8, 9, 14, 7].
I approached this program using an "accumulator" where I would accumulate lists.
My program:
def accumulating():
List = []
Strings = input("Please enter a list of strings: ")
List = Strings.split(" ")
return List
def length(n):
r = []
for i in n:
r.append(len(n))
return r
def main():
y = accumulating()
x = length(y)
print(x)
main()
def accumulating(strings):
return [len(i) for i in strings]
That's about it.
TigerhawkT3 has the right answer, but if u want to change your code you can just do this. In your length function you do not return the lengths of the strings, you just print them. Change it to:
def length(n):
r = []
for i in n:
r.append(len(n))
return r
and
def accumulating():
list = []
strings = input("Please enter a list of strings(seperated by a white space): ")
list = strings.split(" ")
return list
please use cammelcase in variable names, start with lower case. This will avoid mixing variables and datatypes.
http://en.wikipedia.org/wiki/CamelCase
This is the basic logic:
x = ["apple pie", "brownies","chocolate","dulce de leche","eclairs"]
y = []
for i in x:
a = len(i)
y.append(a)
print y
This is the same logic with user input:
b = raw_input("Please enter a list of strings(seperated by a comma): ")
x = []
x.append(b)
x = b.split(",")
y = []
for i in x:
i = i.strip()
a = len(i)
y.append(a)
print y
I use x = b.split(",") so the user input can be separated by a comma, then i = i.strip() will remove the white space so the a = len(i) will be accurate, the white space won't be included in the len.

Categories

Resources