Python
  Home arrow Python arrow Page 4 - Python: More Fun with Strings
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 
IBM Rational Software Development Conference
 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

    Virtual Tradeshows by Ziff Davis Enterprise - A Unique Opportunity to Connect with IT Experts, Access Information, and Gain Insight on today's Technology

    Python: More Fun with Strings - Putting Our Strings to the Test
    (Page 4 of 5 )

    Sometimes it is necessary to test the data in a string. Maybe we want to know if it contains a specific letter or word or sentence, or any text at all. We can perform a true/false boolean test like so:


    >>> cartoon='The Smurfs rock.'

    >>> 'snork' in cartoon

    False

    >>> 'smurf' in cartoon

    False

    >>> 'smurfs' in cartoon

    False

    >>> 'Smurfs' in cartoon

    True

    >>> 'Smurf' in cartoon

    True

    >>> 'rock' in cartoon

    True

    >>> 'The Smurfs' in cartoon

    True

    >>> 'The Smurfs suck' in cartoon

    False

    >>> 'The Smurfs rock' in cartoon

    True

    In the above example we assign the value "The Smurfs rock." to the variable cartoon. We then run a number of tests to see if certain words and eventually sentences appear in the cartoon variable. You will note that the method is case sensitive (it won't find 'smurfs' but it will find 'Smurfs').

    You can also use "not in," to see if text is not in the string, like this:


    >>> 'Snorks' not in cartoon

    True

    Another method of testing whether some text is in a string is to use the find() method, which returns the position of the text in the string:


    >>> cartoon.find('Smurfs')

    4

    >>> cartoon.find('rock')

    11

    >>> cartoon.find('boo')

    -1

    As you can see, the word 'Smurfs' begins at the fourth character, and 'rock' begins at the eleventh character (spaces count as one character). Since the text 'boo' is not in our string, the program returns the value -1.

    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...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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