4
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'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Nikhil Kadam
  • 65
  • 1
  • 1
  • 8

2 Answers2

7

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.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • can you please explain me this statement count = {}.fromkeys(vowels,0) – Nikhil Kadam May 19 '15 at 14:17
  • In the above comment what does { }.fromkeys() means – Nikhil Kadam May 19 '15 at 14:18
  • @NikhilKadam: it is a rather circumspect way of using the [`dict.fromkeys()` class method](https://docs.python.org/2/library/stdtypes.html#dict.fromkeys), it creates a new dictionary with the elements from `vowels` as keys, each with a value set to `0`. – Martijn Pieters May 19 '15 at 14:20
2

In python 2.x you will get an error when you use casefold().

You may just use lower(), these are not the same but comparable.

Read: str.casefold()

Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter 'ß' is equivalent to "ss". Since it is already lowercase, lower() would do nothing to 'ß'; casefold() converts it to "ss".

Ani Menon
  • 27,209
  • 16
  • 105
  • 126