Jun-04-2020, 10:09 PM
(This post was last modified: Jun-04-2020, 10:09 PM by Drone4four.)
@DPaul + @knackwurstbagel: Thanks for your advice. You are both right. I was previously handling a list which cannot be stripped. I’m now working with the
I’m close. I’ve accomplished the first and second objective. But I am still not stripping the trailing dots (.) properly and I’m not appending or extending the results list properly either. Here is my latest attempt that I am working with now:
According to the exercise, the expected out should simply be:
I am so close! Almost there. In my effort to get even closer to the desired end result, I've replaced the last two lines with these three instead:
I’m even closer now! But I'm still not quite where I want to be.
Can someone provide some further hints?
Thanks @Larz60+ for the tip. I’ve replaced the instance of
.join()
method. Only a string can be stripped. I’ve made a little bit more progress. As a refresher, here is what I am trying to do:Quote:- strip off any leading spaces,
- check if the first character is lowercase,
- if so, split the line into words and get the last word,
- strip the trailing dot (.) and exclamation mark (!) from this last word,
- and finally add it to the results list.
I’m close. I’ve accomplished the first and second objective. But I am still not stripping the trailing dots (.) properly and I’m not appending or extending the results list properly either. Here is my latest attempt that I am working with now:
from string import ascii_lowercase text = """ One really nice feature of Python is polymorphism: using the same operation on different types of objects. Let's talk about an elegant feature: slicing. You can use this on a string as well as a list for example 'pybites'[0:2] gives 'py'. The first value is inclusive and the last one is exclusive so here we grab indexes 0 and 1, the letter p and y. When you have a 0 index you can leave it out so can write this as 'pybites'[:2] but here is the kicker: you can use this on a list too! ['pybites', 'teaches', 'you', 'Python'][-2:] would gives ['you', 'Python'] and now you know about slicing from the end as well :) keep enjoying our bites! """ 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] # naive debug: # print(f"Fourth debug: {last_word}") results.extend(last_word) results_as_string = ''.join(results) results_as_string.strip("!").strip(".") # .split("!") print(results_as_string)There is no longer a traceback now. Here is my output:
objects.y.too!:)bites!
According to the exercise, the expected out should simply be:
['objects', 'y', 'too', ':)', 'bites']
I am so close! Almost there. In my effort to get even closer to the desired end result, I've replaced the last two lines with these three instead:
results_as_string.rstrip("!") splitted_result = results_as_string.split('.') print(splitted_result)With these three lines, here is my output:
['objects', 'y', 'too!:)bites!']
I’m even closer now! But I'm still not quite where I want to be.
Can someone provide some further hints?
Thanks @Larz60+ for the tip. I’ve replaced the instance of
strip(".", "!")
with strip(".").strip("!")
. I experimented with using a similar form but instead of using strip
, I used split(".").split("!")
. That didn't work very well.