0
odd_integer = int(input())
integer_list = []

for i in range(0,odd_integer):
    rand_int = int(input()) 
    integer_list.append(rand_int) 

integer_list.reverse()
middle_value = int(len(integer_list)/2)

print(f"{integer_list[0:middle_value]}-[{integer_list[middle_value]}]-{integer_list[middle_value+1:]}")

answer is [2, 5]-[4]-[3, 1]

but it should be [2,5]-[4]-[3,1]

S.B
  • 13,077
  • 10
  • 22
  • 49
  • 1
    Does this answer your question? [Remove all whitespace in a string](https://stackoverflow.com/questions/8270092/remove-all-whitespace-in-a-string) – holydragon Jan 04 '22 at 07:47

4 Answers4

1

You can use replace() in your string

For example:

your_string.replace(", ", ",")

Where according to your question only the ", " (comma followed by space) is replaced by "," (comma).

mouwsy
  • 1,457
  • 12
  • 20
jedymatt
  • 23
  • 7
0

This is due to your print statement. What you can do is first assign it to a variable, replace the spaces and then print it. The replace and print can be combined since replace returns the new string. So basically:

output = f"{integer_list[0:middle_value]}-[{integer_list[middle_value]}]-{integer_list[middle_value+1:]}"
print(output.replace(" ", "")
The Pjot
  • 1,801
  • 1
  • 12
  • 20
0

the space after the coma is the standard representation when you print an array. try to simply print([1,2]) you will have the same result.

if you really need no space you have to rewrite the way to display the array. for example like this :

print(f"{','.join(map(str,integer_list[0:middle_value]))}-[{integer_list[middle_value]}]-{','.join(map(str,integer_list[middle_value+1:]))}")

join function will merge into a string with comma separated as noted, the map function will cast the int into str.

cnaimi
  • 484
  • 6
  • 10
-2

remove multiple space python

import re
re.sub(' +', ' ', 'hello     hi    there')
'hello hi there'

remove after and before space python

st = " a "
strip(st)
#Output : "a"
Saurhub
  • 1
  • 3
  • 1
    I downvoted this answer as it suggests a unnecessarily complicated solution and even importing a package just to replace spaces with nothing when you could do ```st.replace(' ', '')``` as jedymatt suggested in his answer. – Crabby Fish Jan 04 '22 at 07:48
  • 1
    First off, a downvote shouldn't be seen as demotivation, otherwise it wouldn't exist. Downvoting is for when an answer or question should not have been posted, as it might be misleading, off-topic, inefficient, etc.. Second of all, do not pull words out of my mouth. I was suggesting you have a look at jedymatt's answer. I did not ever say or imply I was "so smart". – Crabby Fish Jan 05 '22 at 04:37