3

I'm very new to Python 3 and programming for that matter. I trying to find an algorithm to find out when a sentence is a pangram. I've solved that side of the bargain thanks to a function with the aid of the string module, but I also need to find out how many different letters there are in non-pangrams (switched them to lowercase already).

Any help would be very welcome.

Ubik
  • 139
  • 1
  • 6
Denny Nuyts
  • 165
  • 2
  • 12

3 Answers3

3

Try looking at the length of the set of letters:

len(set(yourstring))


In [8]: set('lowercase')
Out[8]: set(['a', 'c', 'e', 'l', 'o', 's', 'r', 'w'])

In [9]: len(set('lowercase'))
Out[9]: 8

 

Don't forget to remove spaces.

In [10]: len(set('The quick brown fox jumps over the lazy dog'.lower().replace(' ', '')))
Out[10]: 26
Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
1

If you don't want to count punctuation, try this:

s = "It's the economy, stupid!"
t = set(c.lower() for c in s if c.isalnum())
len(t) #returns 13

Or, if you only want letters:

s = "It's the economy, stupid!!!11!!"
t = set(c.lower() for c in s if c.isalpha())
len(t) #returns 13
askewchan
  • 45,161
  • 17
  • 118
  • 134
0

You can try this:

s = 'The quick brown fox jumps over the lazy dog'
count = len(set(c.lower() for c in s if c != ' '))

Here, count will be 26.

Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85