-3

I have string array which looks like below,

THIS IS TO TEST                                                             
USING    APPLE                                                              
ENTER A  LINK                                                               

I need to remove the spaces from the string, so that final string will be like

THIS IS TO TEST USING    APPLE ENTER A  LINK

I Tried below code

for s in htmljob_arr:
    s = s.replace(" ","")
    s = s.strip() + " "
    final_htmljob_str += s 

Is there any better approach to remove the space between the strings?

vishal
  • 309
  • 3
  • 6
  • 21
  • What exactly is `htmljob_arr`? A list? A file? I assume it's not a NumPy array... – CrazyChucky Jun 09 '23 at 15:11
  • `# Define the string to be processed in entry variable. delimiter = " " # Define the delimiter: 60 consecutive spaces. output = entry.replace(delimiter * 60, "") # Replace that consecutive spaces with nothing. output = output.replace(" ", "") # You have something at the end to remove. output = output.replace("\n", " ") # Replace all new lines with space. print(output) # Print the output.` – nycaff Jun 09 '23 at 15:13
  • @nycaff That looks like an answer, and is pretty hard to read without line breaks. Also judging by the asker's code, I don't think the original input is one big string. – CrazyChucky Jun 09 '23 at 15:18
  • Try: strg = """THIS IS TO TEST USING APPLE ENTER A LINK""" print(" ".join(strg.replace(' ','').split())) – Hermann12 Jun 09 '23 at 16:53

2 Answers2

1

You could use str.join:

" ".join([s.replace(" ","").strip() for s in htmljob_arr])

Result

THIS IS TO TEST USING    APPLE ENTER A  LINK

Or for extra credit using re:

re.sub("\s* ", "", " ".join(htmljob_arr))
Jab
  • 26,853
  • 21
  • 75
  • 114
  • 1
    Just to make it perfect, there's no need to create an actual list. You can remove `[]` to use "generator comprehension". – Yevhen Kuzmovych Jun 09 '23 at 15:04
  • @YevhenKuzmovych Using a list comprehension is [more efficient](https://stackoverflow.com/questions/37782066/list-vs-generator-comprehension-speed-with-join-function) when using join – Jab Jun 09 '23 at 15:07
  • Beware of premature optimization, guys. This code isn't sending rockets to the moon. It's usually more confusing to inundate new users with all these clever solutions. – Random Davis Jun 09 '23 at 15:08
  • This isn't confusing as i see it. Perfectly readable imho – Jab Jun 09 '23 at 15:09
  • I think you don’t need: lfor s in htmljob_arr])”, replace in string and strip() is enough. – Hermann12 Jun 09 '23 at 16:56
0

Assuming this is the format of your list:

strings = [
"THIS IS TO TEST                                                             ",
"USING    APPLE                                                              ",
"ENTER A  LINK                                                               "]

We can use list comprehension to remove the extra characters and leave it in a list format:

cleaned_strings = [s.replace(' ', '').strip() for s in strings]

# Result: ['THIS IS TO TEST', 'USING    APPLE', 'ENTER A  LINK']
Random Davis
  • 6,662
  • 4
  • 14
  • 24