Python
  Home arrow Python arrow Page 4 - 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 - 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
     

       

    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 5 Hosted by Hostway
    Stay green...Green IT