[Tutor] (no subject)

Glen Wheeler gew75 at hotmail.com
Tue Jun 1 22:18:00 EDT 2004

  Hi Dragonfirebane,

> I'm having some trouble understanding the use of the re module in
searches, etc.
> What I am trying to do in the attached code is get the user's first and
last name
> with a maximum of 6 characters each (I don't know how to get only the
first six
> characters of each name (first, last). I'll probably divide that into one
for each
> name to make it easier) and find the number of vowels in it.  I am utterly
> regarding how to do this . . . any help would be appreciated.

  Well, for such a simple task I would not use the re module.  Regular
expressions can be hard to understand, and especially for someone who is
relatively new to programming.  More intuitive is to roll your own!

  The standard string object has plenty of builtin functionality.  If you
want to only get names of maximum 6 characters, then use a while loop with
len(name) as a check.  For example:

name = raw_input("Please enter your name > ")
while len(name) > 6:
    print "Six characters maximum.  Please try again."
    name = raw_input("Please enter your name > ")

  Or if you want to get the first six characters of the input string, use a
slice.  For example:

>>> name = "Jimbiusmongius"
>>> shortname = name[:6]
>>> shortname

  To find the number of vowels in a name, try something like :

>>> vowels = ['a', 'e', 'i', 'o', 'u']
>>> name = "Alista"
>>> for letter in name:
..  if letter.lower() in vowels:
..   vowelcount = vowelcount + 1
>>> vowelcount

  The reason .lower() is required is to ensure that the name contains no
uppercase letters, which are not present in our vowels list, since uppercase
and lowercase are not the same according to python.


More information about the Tutor mailing list