Python
  Home arrow Python arrow Page 4 - Python Strings: Spinning Yarns
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 Strings: Spinning Yarns
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2008-02-11

    Table of Contents:
  • Python Strings: Spinning Yarns
  • The Interpolation Operator
  • Changing Strings with Methods
  • Dealing with Multiple Words

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Python Strings: Spinning Yarns - Dealing with Multiple Words
    (Page 4 of 4 )

    Here is an example where a word appears a number of times:


    story='It was a dark and stormy night. There was no light, so it was really dark. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother dark. Dark.'

    print story

    story=story.replace('dark', 'light')

    print story

    When we run the code, this is what we get:

      It was a dark and stormy night. There was no light, so it was really dark. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother dark. Dark.

      It was a light and stormy night. There was no light, so it was really light. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother light. Dark.

    As you can see, it replaced all instances of the word dark, except the one with the capital D, with light.

    This could of course be a problem if we only wanted to replace one specific dark. What if we wanted to change the first word 'dark' to black only? Here is how we would do so:


    story='It was a dark and stormy night. There was no light, so it was really dark. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother dark. Dark.'

    print story

    where=story.find('dark')

    print where

    story=story[:where] + 'black and ' + story[(where+9):]

    print story

    Resulting in:

      It was a dark and stormy night. There was no light, so it was really dark. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother dark. Dark.

      9

      It was a black and stormy night. There was no light, so it was really dark. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother dark. Dark.

    If we only wanted to replace one of the 'dark' words, we could do it this way as well: (type this into your command prompt)


    >>> story.replace('dark','black',1)

    'It was a black and stormy night. There was no light, so it was really dark. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother dark. Dark.'

    Well that's all the time we have for now. In our upcoming tutorials we will discuss the various string methods in depth and take a look at math and numbers in Python.

    Till then...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Thanks for stopping by to read my article on Python Strings, where we go a little...
     

       

    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 6 hosted by Hostway