Advertisement
AceScottie

string reverser with symbol context

Apr 19th, 2019
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. import re
  2. test = "I love python3 programming and my email-id is abc@gmail.com"
  3. newstr = ""
  4. y = re.split(r"[\s@.]+", test) ##splits into a list of words by regual expresion.
  5. x = re.findall(r"[\s@.]+", test)## splits into list of special characters by regualr expression.
  6. for i in range(len(x)):
  7.     if x[i] != " ": ##makes sure the character is not a space
  8.         y[i] = x[i]+y[i] ##fixes the words to add symbols to the start of the word "abc@" -> "@abc". It becomes correct after reversing "@abc" -> "cba@".
  9. for word in y:
  10.     for i in range(len(word)): ##for character in word
  11.         newstr += (word[len(word)-i-1:len(word)-i]) ##reverse the order of the characters using substrings
  12.     if r"".join(re.split(r"[\w']+", newstr[-1:])) == "": #if the last letter of the line is not a symbol add a space.
  13.         newstr += " "
  14. print(newstr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement