Python
  Home arrow Python arrow Page 2 - Python: More Fun with Strings
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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: starstarstarstarstar / 5
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT