AttributeError: 'str' object has no attribute 'pop'

python attributeerror object has no attribute
raise attributeerror
attributeerror django
except attributeerror
valueerror python
how to fix attribute error in python
how to remove attribute error in python
attributeerror python meaning

Error trace:

C:\Users\Abhi.Abhi-PC\Desktop\PYE>ex25ex.py Traceback (most recent
call last):   File "C:\Users\Abhi.Abhi-PC\Desktop\PYE\ex25ex.py", line
41, in <module>     print_last_word(sentence)   File
"C:\Users\Abhi.Abhi-PC\Desktop\PYE\ex25ex.py", line 17, in
print_last_word     word = words.pop(1) AttributeError: 'str' object
has no attribute 'pop'

Here is the code

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split()
    return words

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print word

def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(1)
    print word

def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)

sentence="island has lush vegetation, area. However, summers are cooler than those  abundant."

break_words(sentence)
sort_words(sentence)
print_last_word(sentence)
print_last_word(sentence)
print_first_and_last_sorted(sentence)
print_first_and_last(sentence)

I am unable to figure the issue with the problem. I am working from the book "How to learn Python hard way".

The problem with your code is that you ignore the return values of your functions, especially break_words, and always operate over the original input. This would fix the problem:

words = break_words(sentence)
sort_words(words)
print_first_word(words)
print_last_word(words)
print_first_and_last_sorted(sentence)
print_first_and_last(sentence)

There is another small issue that you should use words.pop(-1) to access the last item in the words list.

How could you have identified the problem from the error message. The exception is an AttributeError that states that 'str' object has no attribute 'pop'. So apparently, in the line words.pop(0), words is a string, not a list of strings, as the variable name suggests. It then only needs a quick glance to see why this variable is a string: when you call the function with print_last_word(sentence) you pass it the original data (of type str) rather than the preprocessed data that you might have intended to pass.

6. Built-in Exceptions — Python 2.7.18 documentation, exception AttributeError �. Raised when an attribute reference (see Attribute references) or assignment fails. (When an object does not support� The AttributeError in Python is raised when an invalid attribute reference is made, or when an attribute assignment fails. While most objects support attributes, those that do not will merely raise a TypeError when an attribute access attempt is made.

What you are trying to do here, is remove a word from a string. When you try to sentence.pop(), you are trying to remove an element from an Immutable Sequence Type. Which means that you are trying to remove an element from a string, which does not make too much sense. What you should instead do, is break the string into words by words.split(' ') and then removing whichever element you want to remove from it. So a solution to replace line 17 is:

word = words.split(' ').pop(len(words.split(' '))-1)

That will remove the last word from the sentence and assign it to word.

Note: This is obviously not the most efficient solution. I broke it down to make it readable.

Hope this helps

Python Exception Handling - AttributeError, The AttributeError in Python is raised when an invalid attribute reference is made, or when an attribute assignment fails. While most objects� AttributeError: 'Layer' object has no attribute 'metadata' from ArcPy with ArcGIS Pro. Ask Question Asked today. Active today. Viewed 16 times 3. I am trying to

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split()
    return words

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print(word)

def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(-1)
    print(word)

def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)

sentence="island has lush vegetation, area. However, summers are cooler than those  abundant."

wordList=break_words(sentence)
print('')
print('wordList:',wordList)
print('')
print('Sorted :',sort_words(wordList))
print('')
print_first_word(wordList)
print('')
print_last_word(wordList)
print('')
print_first_and_last_sorted(sentence)
print('')
print_first_and_last(sentence)

RESULT

wordList: ['island', 'has', 'lush', 'vegetation,', 'area.', 'However,', 'summers', 'are', 'cooler', 'than', 'those', 'abundant.']

Sorted : ['However,', 'abundant.', 'are', 'area.', 'cooler', 'has', 'island', 'lush', 'summers', 'than', 'those', 'vegetation,']

island

abundant.

However,
vegetation,

island
abundant.

Python: AttributeError, These errors yields to the program not executed. One of the error in Python mostly occurs is “ AttributeError “. AttributeError can be defined as an error that is raised� Recent in Machine Learning. problem with Found input variables with inconsistent numbers of samples: [1204, 134] 1 day ago Where is the Keras configuration file stored? 6 days ago

See pop() is not a string function so going forward you can create a function, pass n as the index of the character you need to pop out of the string as str. write below code into the popout:

def popout(str,n):
  front = str[:n]   # up to but not including n
  back = str[n+1:]  # n+1 through end of string
  return front + back

This might answer your question as per my understanding. if not, please correct me in the comment section.

Error Encyclopedia, Attribute Error. Attributes in Python. I usually think about attributes as nouns that belong to an object. For example, “the student has two eyes”. But in Python, an� AttributeError: 'module' object has no attribute 'next' #2852 tacaswell merged 1 commit into matplotlib : master from mdboom : six-minimum-version Mar 8, 2014 Conversation 2 Commits 1 Checks 0 Files changed

Why does this AttributeError in python occur?, This happens because the scipy module doesn't have any attribute named sparse . That attribute only gets defined when you import� AttributeError: module 'asyncio' has no attribute 'coroutine' (Python 3.6.4) -1 AttributeError: Why does the module first imported not register as an attribute?

Python AttributeError — What is it and how do you fix it?, There can be different reasons for attribute error in Python and to fix the error depends on the source of the error. Importing a package will not automatically� Note. It should not be used to indicate that an operator or method is not meant to be supported at all – in that case either leave the operator / method undefined or, if a subclass, set it to None.

How to resolve an attribute error in Python, AttributeError(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You may� Getting AttributeError: 'module' object (scipy) has no attribute 'misc' in Python. How to solve this? >>> import scipy >>> scipy.misc Traceback (most recent call

Comments
  • as the error says, you are trying to call pop() on the str but this function is not available with stings
  • sentence is a string but in print_last_word you might expect it to be list there for the function to work ... also for last word you might want to make words.pop(-1) instead of 1.
  • what should I do then to use pop?
  • make use of break_words that you have written to first convert it to list then use pop()
  • thanks guys problem has been solved
  • thanks!! I got your point. But the problem has not been solved. Now also, it showing the same error.