Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While using casefold(), I am getting an error as " AttributeError: 'str' object has no attribute 'casefold' "

vowels = 'aeiou'

# take input from the user
ip_str = raw_input("Enter a string: ")

# make it suitable for caseless comparisions
ip_str = ip_str.casefold()

# make a dictionary with each vowel a key and value 0
count = {}.fromkeys(vowels,0)

# count the vowels
for char in ip_str:
    if char in count:
        count[char] += 1

print(count)

Error:

    Line - ip_str = ip_str.casefold()
AttributeError: 'str' object has no attribute 'casefold'
like image 634
Nikhil Kadam Avatar asked Feb 11 '23 02:02

Nikhil Kadam


1 Answers

Python 2.6 doesn't support the str.casefold() method.

From the str.casefold() documentation:

New in version 3.3.

You'll need to switch to Python 3.3 or up to be able to use it.

There are no good alternatives, short of implementing the Unicode casefolding algorithm yourself. See How do I case fold a string in Python 2?

However, since you are handling a bytestring here (and not Unicode), you could just use str.lower() and be done with it.

like image 147
Martijn Pieters Avatar answered Feb 15 '23 10:02

Martijn Pieters