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
#9
In the code I provided, replacing extend with append, and printing results seemed to do the trick. I forgot how extend and append works, that was my mistake. In your latest code the whole 'zzz' thing is confusing, I would just use a single strip to get rid of the trailing periods and exclamations. I was reading your transcript and I would like to suggest a few changes.

Quote:24 declare variable line as line with spaces trimmed
30 if last word ends with any combination of ! and . strip it

Why? for 30 In the docs "The chars argument is not a suffix; rather, all combinations of its values are striped"

This can be confusing, I devised a few experiments:

words = ['both!.', 'single.', 'reversed.!', 'exclamation!']
for word in words:
    print(word.rstrip('!.'))

# As comprehension
print([word.rstrip('!.') for word in words])
Can you guess the output before running this? In either case, if your 'zzz' method works that's great (I've not run that code) but it seems your failing the second test and, I would say that instead of
Quote:re-write my code so that it automatically returns ['word', 'list', 'list'] based on the secondary text string
I would try to debug your code with a debugger either one provided in vscode, PyCharm..., or the one python comes with pdb, I personally use pdb++. Its worth learning and saves a lot of print statements, as usually when your printing the value of variables, its because you suspect it does not contain what you think it does. In pdb you can set a breakpoint and then do display <variable_name> to track the value of that variable as it changes.

But in either case
Quote:At line 19, the function parameter is: text: str = text. How does this work? The variable text makes sense, but how and why does the string class method equal text? Also: What does -> list do?
is python typing

Python is dynamically typed and these are just hints to the user of what types the arguments are and in the case of -> list, that it returns a list. It is just a kind way of telling the programmer who is going to use your function that text is a str type and that the function returns a list. It can be safely removed.
Apologetic Canadian, Sorry about that eh?
Reply


Messages In This Thread
RE: "Slicing and dicing strings" - - PyBite #105 - by knackwurstbagel - Jun-11-2020, 09:28 PM

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