Python - Rear stray character String split

Python may be a popular programming dialect known for its effortlessness and coherence. When working with content handling, one common assignment is part of a string based on a particular delimiter. In any case, a challenge emerges when the delimiter shows up after a few words, creating stray characters. In this article, we will explore three distinctive approaches to part a string with stray characters in Python. We will utilize standard expressions, string control with a brief delimiter, and iterative part methods to attain the required result. These approaches give arrangements to handle stray characters and guarantee precise string parts in Python.

Python-Rear stray character String split

Simplicity and Readability − Python's sentence structure is outlined to be clean and clear, making it simpler to get it and type in code. This straightforwardness makes a difference in actualizing string part strategies, counting taking care of stray characters, in a direct way.

A wealth of Built-in Capacities − Python gives a wealthy set of built-in capacities and strategies that can be utilized for string control. Capacities like part(), supplant(), and connect() are promptly accessible and can be viably utilized to handle stray characters and accomplish the required string part comes about.

Customary Expressions Back − Python's re-module permits effective customary expression operations. Normal expressions give an adaptable and effective way to coordinate and manipulate designs inside strings. Typically, especially valuable when managing complex stray character scenarios.

Adaptability and Customization − Python's string control capabilities can be effectively customized to cater to prerequisites. Whether it's employing a brief delimiter, emphasizing through the string, or utilizing standard expressions, Python permits adaptable usage and customization based on the particular needs of the issue at hand.

Approach 1: Regular Expressions (Regex)


Regular expressions are an effective device for design coordination and string control. We will use the re-module in Python to part the string based on a particular design. In our case, the design will coordinate a period that's not taken after by a space.

  • Step 1 − Import the re-module.

  • Step 2 − Characterize the standard expression design.

  • Step 3 − Utilize the re.split() function to part the string based on the design.

  • Step 4 − Get the required part string.


import re

str_with_stray = "Amazing experience"
pattern = r'\.(?!\s)'

split_str = re.split(pattern, str_with_stray)


['Amazing experience']

Approach 2: String Manipulation with a Temporary Delimiter


In this approach, we'll supplant the stray periods with a temporary delimiter that's unlikely to seem within the unique string. At that point, able to part the string utilizing the brief delimiter and reestablish the periods to their unique positions.

  • Step 1 − Supplant stray periods with a brief delimiter.

  • Step 2 − Use split() method and part the altered string utilizing the brief delimiter.

  • Step 3 − Reestablish the first periods in each word presented in split_str.

  • Step 4 − Print the final result.


str_with_stray = "John is excellent in study"
temp_delimiter = '###'

modified_str = str_with_stray.replace('. ', temp_delimiter)
split_str = modified_str.split(temp_delimiter)
split_str = [word.replace(temp_delimiter, '. ') for word in split_str]



['John is excellent in study']

Approach 3: Iterative Splitting


  • Step 1 − Initialize an empty list to store the part words.

  • Step 2 − Part the string by periods.

  • Step 3 − Emphasize through the part words and check in case each word closes with a stray character.

  • Step 4 − Use a while loop and check the certain condition like a word closes with a stray character, and blend it with the consequent word until no stray character is found.

  • Step 5 − Append the blended word to the list of part words.

  • Step 6 − Get the specified part string.


str_with_stray = " I'm.fine."

split_words = []
words = str_with_stray.split('.')
i = 0

while i < len(words):
   word = words[i].strip()
   while i < len(words) - 1 and word[-1] == '':
      i += 1
      word += '.' + words[i].strip()
   i += 1



['Hello', 'world', 'How', 'are', "you? I'm", 'fine', '']

By understanding these approaches, you'll presently handle circumstances where stray characters influence the required string part comes about in Python.


In conclusion, we have investigated three diverse approaches to handle the challenge of parting a string with stray characters in Python. Normal expressions give an effective and adaptable arrangement for design coordination and string control. String control with a brief delimiter permits us to briefly supplant stray characters, part the string, and after that reestablish the first characters. Finally, the iterative splitting technique makes a difference blend words with stray characters to get the specified part string.

Updated on: 29-Aug-2023


Kickstart Your Career

Get certified by completing the course

Get Started