Python Forum
"Slicing and dicing strings" - - PyBite #105
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Slicing and dicing strings" - - PyBite #105
#6
I see that you have extracted the last word, what happens if you last_word.strip("!") on this. The other thing is I happen to be a member of PyBites and solved that one long ago, I did it a completely different way but I noticed in the problem description
Quote:- strip the trailing dot (.) and exclamation mark (!) from this last word,
that and should be or and have notified the PyBites team.

I would like to note that python documentation on str.strip methodsays
Quote:Return a copy of the string the leading and trailing characters removed.
Let us think about a situation where the line contains both:

results_as_string = 'Sed a tincidunt nisl Mauris!.'
results_as_string.strip('!').strip('.')
What does this really mean. Well if we are to chain the methods, it first try to take '!' off the end of results_as_string but the last character is a '.' so what's does it return? It returns the same string back, unmodified. Now the second call to strip takes the result of the last strip and then looks for a '.', It finds one and removes it. You would get back 'Sed a tincidunt nisl Mauris!' So the dot was removed but ! was ignored. But looking at the example in the python documentation, it implies that you could provide a string with the characters to be removed like so:

results_as_string = 'Sed a tincidunt nisl Mauris!.'
results_as_string.strip('!.')
So now we understand, for every character you want to strip, put them all in a string. Ah ha! Ok but now why are we adding the last_word to results before striping '!' or '.' from the ending. Lets try something like this:

results = []
stripped = text.strip()
splitted = stripped.split("\n")
# naive debug:
# print(f"First debug:{splitted}")
for line in splitted:
   # strip off any leading spaces:
   line.lstrip(' ')
   line.rstrip(' ')
   # naive debug:
   # print(f"Second debug: {line}")
   # check if the first character is lowercase:
   if line[0].islower():
       # split the line into words and get the last word:
       new_line_split = line.split()
       # naive debug:
       # print(f"Third debug: {new_line_split}")
       last_word = new_line_split[-1]
       last_word_striped = last_word.strip('!.')
       # naive debug:
       # print(f"Fourth debug: {last_word} {last_word_striped}")
       results.extend(last_word_striped)
       results_as_string = ''.join(results)

print(results_as_string)
Striping it before adding it to the results. Now I have not run this code so I do not know if it all works but it should or be pretty close to working. Let me know.
Reply


Messages In This Thread
RE: "Slicing and dicing strings" - - PyBite #105 - by knackwurstbagel - Jun-04-2020, 10:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary lookups exercise (PyBite #109) Drone4four 14 6,311 Aug-10-2020, 06:41 PM
Last Post: Intr0spective
  Type conversion and exception handling (PyBite #110) Drone4four 5 39,507 Jul-27-2020, 11:33 AM
Last Post: DeaD_EyE
  Iterating over dictionaries with the namedtuple function (PyBite #108) Drone4four 7 5,027 Jul-15-2020, 04:23 AM
Last Post: ndc85430
  Strings inside other strings - substrings OmarSinno 2 3,783 Oct-06-2017, 09:58 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020