Jun-04-2020, 12:58 PM
I’m learning how to ‘slice and dice’ strings. Here is the code challenge:
Here is my code on the first attempt:
When I run the script, I get a traceback pointing to the last line indicating an “AttributeError: ‘list’
object has no attribute 'strip'”. Since strings are ‘immutable’, they can’t be stripped in their place but
That is my main question. I need a few hints from you people to overcome this traceback, without you solving it completely for me.
Also, if you people have any further comments or hints on how to improve my script in other areas, that would be appreciated too.
Here is the full traceback (including print debugging output for further hints at what is wrong with my script):
Thank you.
Quote:https://codechalleng.es/bites/105/
Take the block of text provided and strip off the whitespace at both ends. Split the text by newline (\n).
Loop through the lines, for each line:
- 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.
Return the results list.
Here is my code on the first attempt:
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() last_word = new_line_split[-1] # naive debug: print(f"Third debug: {last_word}") # strip the trailing dot (.) and exclamation mark (!) from this last word: new_line_stripped = new_line_split.strip(".", "!") results = new_line_stripped.split()I’ve done my best to use annotations to explain my rationale for using the class methods and operators that I do and why.
When I run the script, I get a traceback pointing to the last line indicating an “AttributeError: ‘list’
object has no attribute 'strip'”. Since strings are ‘immutable’, they can’t be stripped in their place but
new_line_split
is not a string, it’s a list which I created a few lines previous. Why am I getting this traceback? My own (incorrect) answer to this question is that I am getting this AttributeError because I’m trying to modify a string which can’t be modified. But it’s not a string as far as I can tell - - I am deliberately manipulating a list and assigning it to the variable: new_line_stripped
. But this understanding here is flawed. Could someone please clarify further? That is my main question. I need a few hints from you people to overcome this traceback, without you solving it completely for me.
Also, if you people have any further comments or hints on how to improve my script in other areas, that would be appreciated too.
Here is the full traceback (including print debugging output for further hints at what is wrong with my script):
Quote:$ python slicing_basic.py
First debug:['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!']
Second debug: One really nice feature of Python is polymorphism: using the same operation
Second debug: on different types of objects.
Third debug: objects.
Traceback (most recent call last):
File "slicing_basic.py", line 36, in <module>
new_line_stripped = new_line_split.strip(".", "!")
AttributeError: 'list' object has no attribute 'strip'
Thank you.