This question already has answers here:
How can I suppress the newline after a print statement? [duplicate]
(5 answers)
Closed 3 years ago.
I am attempting to print a reversed array (list) of integers as a single line of space-separated numbers.
I've tried to use a for loop and printed each integer separately with a sep = ", " to remove the commas from the list. Should I use the remove() method as one alternative?
n = int(input())
arr = list(map(int, input().rstrip().split()))
for i in arr[::-1]:
print(i, sep = ", ")
Output
2
3
4
1
Should be:
2 3 4 1
Suppose I inputted 4. The expected results should be the exact sequence in the output but in a single line.
IIUC, this is what you look for:
print(*reversed(arr))
If a comma separator is needed, you can write
print(*reversed(arr), sep=", ")
for i in arr[::-1]:
print(i, end = ", ")
Related
This question already has answers here:
What is the pythonic way to count the leading spaces in a string?
(7 answers)
Closed last year.
Is there a way to get the first letter index of a string after an n number of spaces?
I know it is easily made with a for loop, but for sake of simplicity, is there a cleaner way?
string_1 = " Hello" # Return 4
string_2 = " Bye" # Return 8
Method strip() cuts spaces before the first and after the last charactes (and multiple inside like x y.
So:
x = ' abc'
x = x.strip()
print(x) # result -> 'abc'
You can use filter, enumerate, and next to stop whevener the condition is met:
string_1 = " Hello"
next(filter(lambda x: x[1]!=' ', enumerate(string_1)))[0]
# 4
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 1 year ago.
I need your help Senpai!
How can I print 'n' number of series in a consecutive order?
Basically, I want to print 'n' numbers in series without spaces.
For example in my code,
for x in range(1, n+1):
print(x)
Output for n=5
1
2
3
4
5
But i want to get output like:
12345 (without any spaces in a single line).
Help me with this please!
Use:
n = 10
print(*range(1,n+1), sep="")
print(*range(1,n+1)) is equivalent of print(1, 2, ..., n)
And you need sep="" so it print doesn't displayadditional new line characters between the numbers
This question already has answers here:
Split a string with unknown number of spaces as separator in Python [duplicate]
(6 answers)
Closed 4 years ago.
This function takes one string parameter. Assume the string will be a series of integers separated by spaces. Ignore any extra whitespace. The empty string or a whitespace string return the empty string. Otherwise, the function returns a string with the argument’s integers
separated by spaces but now in sorted order. Do not check for invalid strings. For instance, if the argument is 43 -1 17, the function returns -1 17 43.`
it does not work in a situation where the input is \t42 4 -17 \n
def sort_int_string(string):
strlist = string.split(' ')
new = []
for value in strlist:
value2 = int(value)
new.append(value2)
new = sorted(new)
strlist2 = []
for number in new:
number = str(number)
strlist2.append(number)
final = ' '.join(strlist2)
return final
based on your comment, change the line:
strlist = string.split(' ')
to
strlist = string.split()
That should work because when sep is not specified, it defaults to white space. [ \t\n\r\f\v] are all white spaces.
#VanTan has explained the problem with your code. But you can also use:
x = '43 -1 17'
res = ' '.join(map(str, sorted(map(int, x.split()))))
# '-1 17 43'
This list comprehension should handle the whitespace you are encountering:
s = "\t42 4 -17 \n"
sorted([int(x) for x in " ".join(s.split()).split()])
[-17, 4, 42]
This question already has answers here:
Print in one line dynamically [duplicate]
(22 answers)
Closed 4 years ago.
array = [1,2,3]
print("List: ")
for i in array:
print(i, end=" ")
-------
I want the output to look like List: 1 2 3
How can I achieve this in Python?
Give the value for end parameter in the first print statement
array = [1,2,3]
print("List: ",end="")
for i,x in enumerate(array):
print(array[i], end=" ") if len(array) != x else print(array[i])
print("foo")
Output
List: 1 2 3
foo
print(f"List: {array[0]} {array[1]} {array[2]}")
Maybe :
>>> a = [1,2,3]
>>> b = [str(i) for i in a]
>>> print "List :"+", ".join(b)
List :1, 2, 3
This question already has answers here:
Extract elements of list at odd positions
(5 answers)
Closed 7 years ago.
I have to do this for school, but I can't work it out. I have to get an input and print ONLY the odd characters. So far I've put the input in a list and I have a while loop (which was a clue on the task sheet), but I can't work it out. Please help:
inp = input('What is your name? ')
name = []
name.append(inp)
n=1
while n<len(name):
print inp[1::2]
I guess thats all you need
You don't need to put the string in a list, a string is already essentially a list of characters (more formally, it is a "sequence").
You can use indexing and the modulus operator (%) for this
inp = input('What is your name? ')
n = 0 # index variable
while n < len(inp):
if n % 2 == 1: # check if it is an odd letter
print(inp[n]) # print out that letter
n += 1