Redwood
Redwood

Reputation: 69292

How do I iterate through a string in Python?

As an example, lets say I wanted to list the frequency of each letter of the alphabet in a string. What would be the easiest way to do it?

This is an example of what I'm thinking of... the question is how to make allTheLetters equal to said letters without something like allTheLetters = "abcdefg...xyz". In many other languages I could just do letter++ and increment my way through the alphabet, but thus far I haven't come across a way to do that in python.

def alphCount(text):
  lowerText = text.lower()
  for letter in allTheLetters:  
    print letter + ":", lowertext.count(letter)

Upvotes: 40

Views: 69717

Answers (10)

Jacob Krall
Jacob Krall

Reputation: 28825

Something like this?

for letter in range(ord('a'), ord('z') + 1):
  print chr(letter) + ":", lowertext.count(chr(letter))

Upvotes: 3

guest
guest

Reputation: 1

Just use:

import string
string.lowercase  
string.uppercase

or

string.letters[:26]  
string.letters[26:]

Upvotes: 0

user1956583
user1956583

Reputation: 1

How about this, to use letters, figures and punctuation (all usable to form a Django key):

import random
import string

chars = string.letters + string.digits + string.punctuation
chars_len = len(chars)
n = 40

print(''.join([chars[random.randint(0, chars_len)] for i in range(n)]))

Example result: coOL:V!D+P,&S*hzbO{a0_6]2!{4|OIbVuAbq0:

Upvotes: 0

masnun
masnun

Reputation: 11906

This is what I do:

import string
for x in list(string.lowercase):
    print x

Upvotes: -1

Kimvais
Kimvais

Reputation: 39538

For counting objects, the obvious solution is the Counter

from collections import Counter
import string

c = Counter()
for letter in text.lower():
    c[letter] += 1

for letter in string.lowercase:
    print("%s: %d" % (letter, c[letter]))

Upvotes: 4

mhawke
mhawke

Reputation: 87054

Main question is "iterate through the alphabet":

import string
for c in string.lowercase:
    print c

How get letter frequencies with some efficiency and without counting non-letter characters:

import string

sample = "Hello there, this is a test!"
letter_freq = dict((c,0) for c in string.lowercase)

for c in [c for c in sample.lower() if c.isalpha()]:
    letter_freq[c] += 1

print letter_freq

Upvotes: 2

gimel
gimel

Reputation: 86344

Do you mean using:

import string
string.ascii_lowercase

then,

counters = dict()
for letter in string.ascii_lowercase:
    counters[letter] = lowertext.count(letter)

All lowercase letters are accounted for, missing counters will have zero value.

using generators:

counters = 
    dict( (letter,lowertext.count(letter)) for letter in string.ascii_lowercase )

Upvotes: 3

Glyph
Glyph

Reputation: 31860

The question you've asked (how to iterate through the alphabet) is not the same question as the problem you're trying to solve (how to count the frequency of letters in a string).

You can use string.lowercase, as other posters have suggested:

import string
allTheLetters = string.lowercase

To do things the way you're "used to", treating letters as numbers, you can use the "ord" and "chr" functions. There's absolutely no reason to ever do exactly this, but maybe it comes closer to what you're actually trying to figure out:

def getAllTheLetters(begin='a', end='z'):
    beginNum = ord(begin)
    endNum = ord(end)
    for number in xrange(beginNum, endNum+1):
        yield chr(number)

You can tell it does the right thing because this code prints True:

import string
print ''.join(getAllTheLetters()) == string.lowercase

But, to solve the problem you're actually trying to solve, you want to use a dictionary and collect the letters as you go:

from collections import defaultdict    
def letterOccurrances(string):
    frequencies = defaultdict(lambda: 0)
    for character in string:
        frequencies[character.lower()] += 1
    return frequencies

Use like so:

occs = letterOccurrances("Hello, world!")
print occs['l']
print occs['h']

This will print '3' and '1' respectively.

Note that this works for unicode as well:

# -*- coding: utf-8 -*-
occs = letterOccurrances(u"héĺĺó, ẃóŕĺd!")
print occs[u'l']
print occs[u'ĺ']

If you were to try the other approach on unicode (incrementing through every character) you'd be waiting a long time; there are millions of unicode characters.

To implement your original function (print the counts of each letter in alphabetical order) in terms of this:

def alphCount(text):
    for character, count in sorted(letterOccurrances(text).iteritems()):
        print "%s: %s" % (character, count)

alphCount("hello, world!")

Upvotes: 72

Adam Pierce
Adam Pierce

Reputation: 34345

If you just want to do a frequency count of a string, try this:

s = 'hi there'
f = {}

for c in s:
        f[c] = f.get(c, 0) + 1

print f

Upvotes: 9

Matthew Trevor
Matthew Trevor

Reputation: 14952

the question is how to make allTheLetters equal to said letters without something like allTheLetters = "abcdefg...xyz"

That's actually provided by the string module, it's not like you have to manually type it yourself ;)

import string

allTheLetters = string.ascii_lowercase

def alphCount(text):
  lowerText = text.lower()
  for letter in allTheLetters:  
    print letter + ":", lowertext.count(letter)

Upvotes: 14

Related Questions