Splitting a python string at a delimiter but a specific one












6















Is there a way to split a python string without using a for loop that basically splits a string in the middle to the closest delimiter.



Like:



The cat jumped over the moon very quickly.


The delimiter would be the space and the resulting strings would be:



The cat jumped over
the moon very quickly.


I see there is a count where I can see how many spaces are in there (Don't see how to return their indexes though). I could then find the middle one by dividing by two, but then how to say split on this delimiter at this index. Find is close but it returns the first index (or right first index using rfind) not all the indexes where " " is found. I might be over thinking this.










share|improve this question


















  • 2





    What about using split() and the re-joining the first and second half of the resulting list separately?

    – Feodoran
    7 hours ago






  • 2





    The algorithm you define (counting the spaces) would split the sentence into equal number of words, which conflicts with your requirement (split a string in the middle to the closest delimiter). Which one are you after?

    – Selcuk
    7 hours ago
















6















Is there a way to split a python string without using a for loop that basically splits a string in the middle to the closest delimiter.



Like:



The cat jumped over the moon very quickly.


The delimiter would be the space and the resulting strings would be:



The cat jumped over
the moon very quickly.


I see there is a count where I can see how many spaces are in there (Don't see how to return their indexes though). I could then find the middle one by dividing by two, but then how to say split on this delimiter at this index. Find is close but it returns the first index (or right first index using rfind) not all the indexes where " " is found. I might be over thinking this.










share|improve this question


















  • 2





    What about using split() and the re-joining the first and second half of the resulting list separately?

    – Feodoran
    7 hours ago






  • 2





    The algorithm you define (counting the spaces) would split the sentence into equal number of words, which conflicts with your requirement (split a string in the middle to the closest delimiter). Which one are you after?

    – Selcuk
    7 hours ago














6












6








6








Is there a way to split a python string without using a for loop that basically splits a string in the middle to the closest delimiter.



Like:



The cat jumped over the moon very quickly.


The delimiter would be the space and the resulting strings would be:



The cat jumped over
the moon very quickly.


I see there is a count where I can see how many spaces are in there (Don't see how to return their indexes though). I could then find the middle one by dividing by two, but then how to say split on this delimiter at this index. Find is close but it returns the first index (or right first index using rfind) not all the indexes where " " is found. I might be over thinking this.










share|improve this question














Is there a way to split a python string without using a for loop that basically splits a string in the middle to the closest delimiter.



Like:



The cat jumped over the moon very quickly.


The delimiter would be the space and the resulting strings would be:



The cat jumped over
the moon very quickly.


I see there is a count where I can see how many spaces are in there (Don't see how to return their indexes though). I could then find the middle one by dividing by two, but then how to say split on this delimiter at this index. Find is close but it returns the first index (or right first index using rfind) not all the indexes where " " is found. I might be over thinking this.







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









CodejoyCodejoy

1,28483868




1,28483868








  • 2





    What about using split() and the re-joining the first and second half of the resulting list separately?

    – Feodoran
    7 hours ago






  • 2





    The algorithm you define (counting the spaces) would split the sentence into equal number of words, which conflicts with your requirement (split a string in the middle to the closest delimiter). Which one are you after?

    – Selcuk
    7 hours ago














  • 2





    What about using split() and the re-joining the first and second half of the resulting list separately?

    – Feodoran
    7 hours ago






  • 2





    The algorithm you define (counting the spaces) would split the sentence into equal number of words, which conflicts with your requirement (split a string in the middle to the closest delimiter). Which one are you after?

    – Selcuk
    7 hours ago








2




2





What about using split() and the re-joining the first and second half of the resulting list separately?

– Feodoran
7 hours ago





What about using split() and the re-joining the first and second half of the resulting list separately?

– Feodoran
7 hours ago




2




2





The algorithm you define (counting the spaces) would split the sentence into equal number of words, which conflicts with your requirement (split a string in the middle to the closest delimiter). Which one are you after?

– Selcuk
7 hours ago





The algorithm you define (counting the spaces) would split the sentence into equal number of words, which conflicts with your requirement (split a string in the middle to the closest delimiter). Which one are you after?

– Selcuk
7 hours ago












6 Answers
6






active

oldest

votes


















2














how about something like this:



s = "The cat jumped over the moon very quickly"

l = s.split()

s1 = ' '.join(l[:len(l)//2])
s2 = ' '.join(l[len(l)//2 :])

print(s1)
print(s2)





share|improve this answer































    3














    This should work:



    def split_text(text):
    middle = len(text)//2
    under = text.rfind(" ", 0, middle)
    over = text.find(" ", middle)
    if over > under and under != -1:
    return (text[:,middle - under], text[middle - under,:])
    else:
    if over is -1:
    raise ValueError("No separator found in text '{}'".format(text))
    return (text[:,middle + over], text[middle + over,:])


    it does not use a for loop, but probably using a for loop would have better performance.



    I handle the case where the separator is not found in the whole string by raising an error, but change raise ValueError() for whatever way you want to handle that case.






    share|improve this answer





















    • 2





      You mean under = text.rfind(" ", 0, middle).

      – CristiFati
      7 hours ago








    • 1





      Right, I will edit it.

      – spaniard
      6 hours ago






    • 1





      Algorithmically speaking, this is as efficient as it can get.

      – Olivier Melançon
      6 hours ago






    • 2





      @spaniard Although, I think you have to handle the case where find and rfind will return -1

      – Olivier Melançon
      6 hours ago






    • 2





      @OlivierMelançon right! thanks I will fix it.

      – spaniard
      6 hours ago



















    2














    I'd just split then rejoin:



    text = "The cat jumped over the moon very quickly"
    words = text.split()
    first_half = " ".join(words[:len(words)//2])





    share|improve this answer



















    • 1





      Depends on whether you want to split by # of words or overall string length.

      – Amber
      7 hours ago






    • 1





      This split in equal amount of words, not characters

      – Olivier Melançon
      7 hours ago



















    2














    I think the solutions using split are good. I tried to solve it without split and here's what I came up with.



    sOdd = "The cat jumped over the moon very quickly."
    sEven = "The cat jumped over the moon very quickly now."

    def split_on_delim_mid(s, delim=" "):
    delim_indexes = [
    x[0] for x in enumerate(s) if x[1]==delim
    ] # [3, 7, 14, 19, 23, 28, 33]

    # Select the correct number from delim_indexes
    middle = len(delim_indexes)/2
    if middle % 2 == 0:
    middle_index = middle
    else:
    middle_index = (middle-.5)

    # Return the separated sentances
    sep = delim_indexes[int(middle_index)]
    return s[:sep], s[sep:]

    split_on_delim_mid(sOdd) # ('The cat jumped over', ' the moon very quickly.')
    split_on_delim_mid(sEven) # ('The cat jumped over the', ' moon very quickly now.')


    The idea here is to:




    • Find the indexes of the deliminator.

    • Find the median of that list of indexes

    • Split on that.






    share|improve this answer































      2














      You can use min to find the closest space to the middle and then slice the string.



      s = "The cat jumped over the moon very quickly."

      mid = min((i for i, c in enumerate(s) if c == ' '), key=lambda i: abs(i - len(s) // 2))

      fst, snd = s[:mid], s[mid+1:]

      print(fst)
      print(snd)


      Output



      The cat jumped over
      the moon very quickly.





      share|improve this answer































        1














        Solutions with split() and join() are fine if you want to get half the words, not half the string (counting the characters and not the words). I think the latter is impossibile without a for loop or a list comprehension (or an expensive workaround such a recursion to find the indexes of the spaces maybe).



        But if you are fine with a list comprehension, you could do:



        phrase = "The cat jumped over the moon very quickly."

        #indexes of separator, here the ' '
        sep_idxs = [i for i, j in enumerate(phrase) if j == ' ']

        #getting the separator index closer to half the length of the string
        sep = min(sep_idxs, key=lambda x:abs(x-(len(phrase) // 2)))

        first_half = phrase[:sep]
        last_half = phrase[sep+1:]

        print([first_half, last_half])


        Here first I look for the indexes of the separator with the list comprehension. Then I find the index of the closer separator to the half of the string using a custom key for the min() built-in function. Then split.



        The print statement prints ['The cat jumped over', 'the moon very quickly.']






        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54298939%2fsplitting-a-python-string-at-a-delimiter-but-a-specific-one%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          how about something like this:



          s = "The cat jumped over the moon very quickly"

          l = s.split()

          s1 = ' '.join(l[:len(l)//2])
          s2 = ' '.join(l[len(l)//2 :])

          print(s1)
          print(s2)





          share|improve this answer




























            2














            how about something like this:



            s = "The cat jumped over the moon very quickly"

            l = s.split()

            s1 = ' '.join(l[:len(l)//2])
            s2 = ' '.join(l[len(l)//2 :])

            print(s1)
            print(s2)





            share|improve this answer


























              2












              2








              2







              how about something like this:



              s = "The cat jumped over the moon very quickly"

              l = s.split()

              s1 = ' '.join(l[:len(l)//2])
              s2 = ' '.join(l[len(l)//2 :])

              print(s1)
              print(s2)





              share|improve this answer













              how about something like this:



              s = "The cat jumped over the moon very quickly"

              l = s.split()

              s1 = ' '.join(l[:len(l)//2])
              s2 = ' '.join(l[len(l)//2 :])

              print(s1)
              print(s2)






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 7 hours ago









              LonelyDaoistLonelyDaoist

              1117




              1117

























                  3














                  This should work:



                  def split_text(text):
                  middle = len(text)//2
                  under = text.rfind(" ", 0, middle)
                  over = text.find(" ", middle)
                  if over > under and under != -1:
                  return (text[:,middle - under], text[middle - under,:])
                  else:
                  if over is -1:
                  raise ValueError("No separator found in text '{}'".format(text))
                  return (text[:,middle + over], text[middle + over,:])


                  it does not use a for loop, but probably using a for loop would have better performance.



                  I handle the case where the separator is not found in the whole string by raising an error, but change raise ValueError() for whatever way you want to handle that case.






                  share|improve this answer





















                  • 2





                    You mean under = text.rfind(" ", 0, middle).

                    – CristiFati
                    7 hours ago








                  • 1





                    Right, I will edit it.

                    – spaniard
                    6 hours ago






                  • 1





                    Algorithmically speaking, this is as efficient as it can get.

                    – Olivier Melançon
                    6 hours ago






                  • 2





                    @spaniard Although, I think you have to handle the case where find and rfind will return -1

                    – Olivier Melançon
                    6 hours ago






                  • 2





                    @OlivierMelançon right! thanks I will fix it.

                    – spaniard
                    6 hours ago
















                  3














                  This should work:



                  def split_text(text):
                  middle = len(text)//2
                  under = text.rfind(" ", 0, middle)
                  over = text.find(" ", middle)
                  if over > under and under != -1:
                  return (text[:,middle - under], text[middle - under,:])
                  else:
                  if over is -1:
                  raise ValueError("No separator found in text '{}'".format(text))
                  return (text[:,middle + over], text[middle + over,:])


                  it does not use a for loop, but probably using a for loop would have better performance.



                  I handle the case where the separator is not found in the whole string by raising an error, but change raise ValueError() for whatever way you want to handle that case.






                  share|improve this answer





















                  • 2





                    You mean under = text.rfind(" ", 0, middle).

                    – CristiFati
                    7 hours ago








                  • 1





                    Right, I will edit it.

                    – spaniard
                    6 hours ago






                  • 1





                    Algorithmically speaking, this is as efficient as it can get.

                    – Olivier Melançon
                    6 hours ago






                  • 2





                    @spaniard Although, I think you have to handle the case where find and rfind will return -1

                    – Olivier Melançon
                    6 hours ago






                  • 2





                    @OlivierMelançon right! thanks I will fix it.

                    – spaniard
                    6 hours ago














                  3












                  3








                  3







                  This should work:



                  def split_text(text):
                  middle = len(text)//2
                  under = text.rfind(" ", 0, middle)
                  over = text.find(" ", middle)
                  if over > under and under != -1:
                  return (text[:,middle - under], text[middle - under,:])
                  else:
                  if over is -1:
                  raise ValueError("No separator found in text '{}'".format(text))
                  return (text[:,middle + over], text[middle + over,:])


                  it does not use a for loop, but probably using a for loop would have better performance.



                  I handle the case where the separator is not found in the whole string by raising an error, but change raise ValueError() for whatever way you want to handle that case.






                  share|improve this answer















                  This should work:



                  def split_text(text):
                  middle = len(text)//2
                  under = text.rfind(" ", 0, middle)
                  over = text.find(" ", middle)
                  if over > under and under != -1:
                  return (text[:,middle - under], text[middle - under,:])
                  else:
                  if over is -1:
                  raise ValueError("No separator found in text '{}'".format(text))
                  return (text[:,middle + over], text[middle + over,:])


                  it does not use a for loop, but probably using a for loop would have better performance.



                  I handle the case where the separator is not found in the whole string by raising an error, but change raise ValueError() for whatever way you want to handle that case.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 5 hours ago









                  Olivier Melançon

                  13k11940




                  13k11940










                  answered 7 hours ago









                  spaniardspaniard

                  30717




                  30717








                  • 2





                    You mean under = text.rfind(" ", 0, middle).

                    – CristiFati
                    7 hours ago








                  • 1





                    Right, I will edit it.

                    – spaniard
                    6 hours ago






                  • 1





                    Algorithmically speaking, this is as efficient as it can get.

                    – Olivier Melançon
                    6 hours ago






                  • 2





                    @spaniard Although, I think you have to handle the case where find and rfind will return -1

                    – Olivier Melançon
                    6 hours ago






                  • 2





                    @OlivierMelançon right! thanks I will fix it.

                    – spaniard
                    6 hours ago














                  • 2





                    You mean under = text.rfind(" ", 0, middle).

                    – CristiFati
                    7 hours ago








                  • 1





                    Right, I will edit it.

                    – spaniard
                    6 hours ago






                  • 1





                    Algorithmically speaking, this is as efficient as it can get.

                    – Olivier Melançon
                    6 hours ago






                  • 2





                    @spaniard Although, I think you have to handle the case where find and rfind will return -1

                    – Olivier Melançon
                    6 hours ago






                  • 2





                    @OlivierMelançon right! thanks I will fix it.

                    – spaniard
                    6 hours ago








                  2




                  2





                  You mean under = text.rfind(" ", 0, middle).

                  – CristiFati
                  7 hours ago







                  You mean under = text.rfind(" ", 0, middle).

                  – CristiFati
                  7 hours ago






                  1




                  1





                  Right, I will edit it.

                  – spaniard
                  6 hours ago





                  Right, I will edit it.

                  – spaniard
                  6 hours ago




                  1




                  1





                  Algorithmically speaking, this is as efficient as it can get.

                  – Olivier Melançon
                  6 hours ago





                  Algorithmically speaking, this is as efficient as it can get.

                  – Olivier Melançon
                  6 hours ago




                  2




                  2





                  @spaniard Although, I think you have to handle the case where find and rfind will return -1

                  – Olivier Melançon
                  6 hours ago





                  @spaniard Although, I think you have to handle the case where find and rfind will return -1

                  – Olivier Melançon
                  6 hours ago




                  2




                  2





                  @OlivierMelançon right! thanks I will fix it.

                  – spaniard
                  6 hours ago





                  @OlivierMelançon right! thanks I will fix it.

                  – spaniard
                  6 hours ago











                  2














                  I'd just split then rejoin:



                  text = "The cat jumped over the moon very quickly"
                  words = text.split()
                  first_half = " ".join(words[:len(words)//2])





                  share|improve this answer



















                  • 1





                    Depends on whether you want to split by # of words or overall string length.

                    – Amber
                    7 hours ago






                  • 1





                    This split in equal amount of words, not characters

                    – Olivier Melançon
                    7 hours ago
















                  2














                  I'd just split then rejoin:



                  text = "The cat jumped over the moon very quickly"
                  words = text.split()
                  first_half = " ".join(words[:len(words)//2])





                  share|improve this answer



















                  • 1





                    Depends on whether you want to split by # of words or overall string length.

                    – Amber
                    7 hours ago






                  • 1





                    This split in equal amount of words, not characters

                    – Olivier Melançon
                    7 hours ago














                  2












                  2








                  2







                  I'd just split then rejoin:



                  text = "The cat jumped over the moon very quickly"
                  words = text.split()
                  first_half = " ".join(words[:len(words)//2])





                  share|improve this answer













                  I'd just split then rejoin:



                  text = "The cat jumped over the moon very quickly"
                  words = text.split()
                  first_half = " ".join(words[:len(words)//2])






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 7 hours ago









                  Joe HalliwellJoe Halliwell

                  625317




                  625317








                  • 1





                    Depends on whether you want to split by # of words or overall string length.

                    – Amber
                    7 hours ago






                  • 1





                    This split in equal amount of words, not characters

                    – Olivier Melançon
                    7 hours ago














                  • 1





                    Depends on whether you want to split by # of words or overall string length.

                    – Amber
                    7 hours ago






                  • 1





                    This split in equal amount of words, not characters

                    – Olivier Melançon
                    7 hours ago








                  1




                  1





                  Depends on whether you want to split by # of words or overall string length.

                  – Amber
                  7 hours ago





                  Depends on whether you want to split by # of words or overall string length.

                  – Amber
                  7 hours ago




                  1




                  1





                  This split in equal amount of words, not characters

                  – Olivier Melançon
                  7 hours ago





                  This split in equal amount of words, not characters

                  – Olivier Melançon
                  7 hours ago











                  2














                  I think the solutions using split are good. I tried to solve it without split and here's what I came up with.



                  sOdd = "The cat jumped over the moon very quickly."
                  sEven = "The cat jumped over the moon very quickly now."

                  def split_on_delim_mid(s, delim=" "):
                  delim_indexes = [
                  x[0] for x in enumerate(s) if x[1]==delim
                  ] # [3, 7, 14, 19, 23, 28, 33]

                  # Select the correct number from delim_indexes
                  middle = len(delim_indexes)/2
                  if middle % 2 == 0:
                  middle_index = middle
                  else:
                  middle_index = (middle-.5)

                  # Return the separated sentances
                  sep = delim_indexes[int(middle_index)]
                  return s[:sep], s[sep:]

                  split_on_delim_mid(sOdd) # ('The cat jumped over', ' the moon very quickly.')
                  split_on_delim_mid(sEven) # ('The cat jumped over the', ' moon very quickly now.')


                  The idea here is to:




                  • Find the indexes of the deliminator.

                  • Find the median of that list of indexes

                  • Split on that.






                  share|improve this answer




























                    2














                    I think the solutions using split are good. I tried to solve it without split and here's what I came up with.



                    sOdd = "The cat jumped over the moon very quickly."
                    sEven = "The cat jumped over the moon very quickly now."

                    def split_on_delim_mid(s, delim=" "):
                    delim_indexes = [
                    x[0] for x in enumerate(s) if x[1]==delim
                    ] # [3, 7, 14, 19, 23, 28, 33]

                    # Select the correct number from delim_indexes
                    middle = len(delim_indexes)/2
                    if middle % 2 == 0:
                    middle_index = middle
                    else:
                    middle_index = (middle-.5)

                    # Return the separated sentances
                    sep = delim_indexes[int(middle_index)]
                    return s[:sep], s[sep:]

                    split_on_delim_mid(sOdd) # ('The cat jumped over', ' the moon very quickly.')
                    split_on_delim_mid(sEven) # ('The cat jumped over the', ' moon very quickly now.')


                    The idea here is to:




                    • Find the indexes of the deliminator.

                    • Find the median of that list of indexes

                    • Split on that.






                    share|improve this answer


























                      2












                      2








                      2







                      I think the solutions using split are good. I tried to solve it without split and here's what I came up with.



                      sOdd = "The cat jumped over the moon very quickly."
                      sEven = "The cat jumped over the moon very quickly now."

                      def split_on_delim_mid(s, delim=" "):
                      delim_indexes = [
                      x[0] for x in enumerate(s) if x[1]==delim
                      ] # [3, 7, 14, 19, 23, 28, 33]

                      # Select the correct number from delim_indexes
                      middle = len(delim_indexes)/2
                      if middle % 2 == 0:
                      middle_index = middle
                      else:
                      middle_index = (middle-.5)

                      # Return the separated sentances
                      sep = delim_indexes[int(middle_index)]
                      return s[:sep], s[sep:]

                      split_on_delim_mid(sOdd) # ('The cat jumped over', ' the moon very quickly.')
                      split_on_delim_mid(sEven) # ('The cat jumped over the', ' moon very quickly now.')


                      The idea here is to:




                      • Find the indexes of the deliminator.

                      • Find the median of that list of indexes

                      • Split on that.






                      share|improve this answer













                      I think the solutions using split are good. I tried to solve it without split and here's what I came up with.



                      sOdd = "The cat jumped over the moon very quickly."
                      sEven = "The cat jumped over the moon very quickly now."

                      def split_on_delim_mid(s, delim=" "):
                      delim_indexes = [
                      x[0] for x in enumerate(s) if x[1]==delim
                      ] # [3, 7, 14, 19, 23, 28, 33]

                      # Select the correct number from delim_indexes
                      middle = len(delim_indexes)/2
                      if middle % 2 == 0:
                      middle_index = middle
                      else:
                      middle_index = (middle-.5)

                      # Return the separated sentances
                      sep = delim_indexes[int(middle_index)]
                      return s[:sep], s[sep:]

                      split_on_delim_mid(sOdd) # ('The cat jumped over', ' the moon very quickly.')
                      split_on_delim_mid(sEven) # ('The cat jumped over the', ' moon very quickly now.')


                      The idea here is to:




                      • Find the indexes of the deliminator.

                      • Find the median of that list of indexes

                      • Split on that.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 7 hours ago









                      Charles LandauCharles Landau

                      2,2481216




                      2,2481216























                          2














                          You can use min to find the closest space to the middle and then slice the string.



                          s = "The cat jumped over the moon very quickly."

                          mid = min((i for i, c in enumerate(s) if c == ' '), key=lambda i: abs(i - len(s) // 2))

                          fst, snd = s[:mid], s[mid+1:]

                          print(fst)
                          print(snd)


                          Output



                          The cat jumped over
                          the moon very quickly.





                          share|improve this answer




























                            2














                            You can use min to find the closest space to the middle and then slice the string.



                            s = "The cat jumped over the moon very quickly."

                            mid = min((i for i, c in enumerate(s) if c == ' '), key=lambda i: abs(i - len(s) // 2))

                            fst, snd = s[:mid], s[mid+1:]

                            print(fst)
                            print(snd)


                            Output



                            The cat jumped over
                            the moon very quickly.





                            share|improve this answer


























                              2












                              2








                              2







                              You can use min to find the closest space to the middle and then slice the string.



                              s = "The cat jumped over the moon very quickly."

                              mid = min((i for i, c in enumerate(s) if c == ' '), key=lambda i: abs(i - len(s) // 2))

                              fst, snd = s[:mid], s[mid+1:]

                              print(fst)
                              print(snd)


                              Output



                              The cat jumped over
                              the moon very quickly.





                              share|improve this answer













                              You can use min to find the closest space to the middle and then slice the string.



                              s = "The cat jumped over the moon very quickly."

                              mid = min((i for i, c in enumerate(s) if c == ' '), key=lambda i: abs(i - len(s) // 2))

                              fst, snd = s[:mid], s[mid+1:]

                              print(fst)
                              print(snd)


                              Output



                              The cat jumped over
                              the moon very quickly.






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 6 hours ago









                              Olivier MelançonOlivier Melançon

                              13k11940




                              13k11940























                                  1














                                  Solutions with split() and join() are fine if you want to get half the words, not half the string (counting the characters and not the words). I think the latter is impossibile without a for loop or a list comprehension (or an expensive workaround such a recursion to find the indexes of the spaces maybe).



                                  But if you are fine with a list comprehension, you could do:



                                  phrase = "The cat jumped over the moon very quickly."

                                  #indexes of separator, here the ' '
                                  sep_idxs = [i for i, j in enumerate(phrase) if j == ' ']

                                  #getting the separator index closer to half the length of the string
                                  sep = min(sep_idxs, key=lambda x:abs(x-(len(phrase) // 2)))

                                  first_half = phrase[:sep]
                                  last_half = phrase[sep+1:]

                                  print([first_half, last_half])


                                  Here first I look for the indexes of the separator with the list comprehension. Then I find the index of the closer separator to the half of the string using a custom key for the min() built-in function. Then split.



                                  The print statement prints ['The cat jumped over', 'the moon very quickly.']






                                  share|improve this answer




























                                    1














                                    Solutions with split() and join() are fine if you want to get half the words, not half the string (counting the characters and not the words). I think the latter is impossibile without a for loop or a list comprehension (or an expensive workaround such a recursion to find the indexes of the spaces maybe).



                                    But if you are fine with a list comprehension, you could do:



                                    phrase = "The cat jumped over the moon very quickly."

                                    #indexes of separator, here the ' '
                                    sep_idxs = [i for i, j in enumerate(phrase) if j == ' ']

                                    #getting the separator index closer to half the length of the string
                                    sep = min(sep_idxs, key=lambda x:abs(x-(len(phrase) // 2)))

                                    first_half = phrase[:sep]
                                    last_half = phrase[sep+1:]

                                    print([first_half, last_half])


                                    Here first I look for the indexes of the separator with the list comprehension. Then I find the index of the closer separator to the half of the string using a custom key for the min() built-in function. Then split.



                                    The print statement prints ['The cat jumped over', 'the moon very quickly.']






                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      Solutions with split() and join() are fine if you want to get half the words, not half the string (counting the characters and not the words). I think the latter is impossibile without a for loop or a list comprehension (or an expensive workaround such a recursion to find the indexes of the spaces maybe).



                                      But if you are fine with a list comprehension, you could do:



                                      phrase = "The cat jumped over the moon very quickly."

                                      #indexes of separator, here the ' '
                                      sep_idxs = [i for i, j in enumerate(phrase) if j == ' ']

                                      #getting the separator index closer to half the length of the string
                                      sep = min(sep_idxs, key=lambda x:abs(x-(len(phrase) // 2)))

                                      first_half = phrase[:sep]
                                      last_half = phrase[sep+1:]

                                      print([first_half, last_half])


                                      Here first I look for the indexes of the separator with the list comprehension. Then I find the index of the closer separator to the half of the string using a custom key for the min() built-in function. Then split.



                                      The print statement prints ['The cat jumped over', 'the moon very quickly.']






                                      share|improve this answer













                                      Solutions with split() and join() are fine if you want to get half the words, not half the string (counting the characters and not the words). I think the latter is impossibile without a for loop or a list comprehension (or an expensive workaround such a recursion to find the indexes of the spaces maybe).



                                      But if you are fine with a list comprehension, you could do:



                                      phrase = "The cat jumped over the moon very quickly."

                                      #indexes of separator, here the ' '
                                      sep_idxs = [i for i, j in enumerate(phrase) if j == ' ']

                                      #getting the separator index closer to half the length of the string
                                      sep = min(sep_idxs, key=lambda x:abs(x-(len(phrase) // 2)))

                                      first_half = phrase[:sep]
                                      last_half = phrase[sep+1:]

                                      print([first_half, last_half])


                                      Here first I look for the indexes of the separator with the list comprehension. Then I find the index of the closer separator to the half of the string using a custom key for the min() built-in function. Then split.



                                      The print statement prints ['The cat jumped over', 'the moon very quickly.']







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 7 hours ago









                                      ValentinoValentino

                                      33919




                                      33919






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54298939%2fsplitting-a-python-string-at-a-delimiter-but-a-specific-one%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          サソリ

                                          広島県道265号伴広島線

                                          Setup Asymptote in Texstudio