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
#5
@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 .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.
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary lookups exercise (PyBite #109) Drone4four 14 6,326 Aug-10-2020, 06:41 PM
Last Post: Intr0spective
  Type conversion and exception handling (PyBite #110) Drone4four 5 39,610 Jul-27-2020, 11:33 AM
Last Post: DeaD_EyE
  Iterating over dictionaries with the namedtuple function (PyBite #108) Drone4four 7 5,033 Jul-15-2020, 04:23 AM
Last Post: ndc85430
  Strings inside other strings - substrings OmarSinno 2 3,792 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