Python
  Home arrow Python arrow Page 2 - Python: More Fun with Strings
The Best Selling PC Migration Utility.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
eWeek
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python: More Fun with Strings
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-02-04

    Table of Contents:
  • Python: More Fun with Strings
  • Working with String Methods
  • Capital Idea(s)
  • Putting Our Strings to the Test
  • Converting Data and Sorting

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    The Web Buyer's Guide is your best source for white papers on a wide range of IT products and services. This Week's Featured White Papers: Help Simplify Virtualization by IBM

    Python: More Fun with Strings - Working with String Methods
    (Page 2 of 5 )

    There are many built-in string methods in Python. We will cover a few of them here, and all of them in a future article.

    A method saves the programmer from having to program the same code over and over again. Instead of typing a bunch of code to say, count the number of a's in a string, all the programmer has to do is call the appropriate method. In the following example, we are going to find out how many times each vowel appears in a string. Type the following into your python shell command prompt:


    book='It was a dark and stormy night, the wind blowing through the trees, and somewhere, out there in the far off teinkling of city lights, Britney Spears was busy being a bad mother...'

    book.count('a')

    book.count('e')

    book.count('i')

    book.count('o')

    book.count('u')

    book.count('y')

    The method will count each of the vowels and return a result, like the one below:


    >>> book.count('a')

    10

    >>> book.count('e')

    15

    >>> book.count('i')

    10

    >>> book.count('o')

    8

    >>> book.count('u')

    3

    >>> book.count('y')

    4

    We can also count words within the document, like so:


    >>> book.count('the')

    5

    >>> book.count('and')

    2

    >>> book.count('was')

    2

    More Python Articles
    More By James Payne


       · Hey thanks for stopping by for our continued discussion on String manipulation in...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway