Amado Flores
Amado Flores

Reputation: 31

removeprefix for string object is not an attribute

I'm trying to test out all the methods for a string in order to understand and learn them, and I tried doing the following removeprefix() method, but it was not allowing me to do so. Is there some library problem I am having?

begone = 'begone'

newbegone = begone.removeprefix('be')
print(newbegone)
#print("Removing Prefix 'be':", begone.removeprefix('be'), ', new word')
#print("Removing Prefix 'gone':", begone.removeprefix('gone'), ', original word')

Upvotes: 3

Views: 5021

Answers (1)

khelwood
khelwood

Reputation: 59146

removeprefix and removesuffix were added in Python 3.9. You're using an earlier version.

See PEP-616.

To remove the prefix 'be' from begone in prior versions, you can use a slice:

newbegone = begone[2:]

where 2 is the length of the prefix you are removing.

Upvotes: 3

Related Questions