Python
  Home arrow Python arrow Page 3 - Python Strings: Spinning Yarns
FaxWave - Free Trial.
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 
 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

    Ziff Davis Enterprise Virtual Tradeshows: Hot Topics, Cutting Edge Technology, Real-time Interaction with IT Professionals. Learn more at ziffdavisvts.com

    Python Strings: Spinning Yarns - Changing Strings with Methods
    (Page 3 of 4 )

    We can use various methods to change the data in a string. We touched on slicing and concatenation earlier in the article, and I am going to talk about it just a little more here. Before, we replaced an entire word; this time we will only replace part of a word:

    speech='A toast, to bread, whom without there would be no taste'

    what='No wait that is not right'

    print speech

    print what

    speech=speech[0:51] + 'oa' + speech[52:54]

    print speech

    Which would produce the following output:

      A toast, to bread, whom without there would be no taste

      No wait that is not right

      A toast, to bread, whom without there would be no toast

    We could also use the replace method, which might prove to be a little easier:


    speech='A toast, to bread, whom without there would be no taste'

    what='No wait that is not right'

    print speech

    print what

    speech=speech.replace('taste','toast')

    print speech

    Resulting in:

      A toast, to bread, whom without there would be no taste

      No wait that is not right

      A toast, to bread, whom without there would be no toast

    This would also be handy if, say, a certain writer by the name of James Payne began dating your mother:


    oldFather='Your father is named...whatshisname...'

    print oldFather

    oldFather=oldFather.replace('whatshisname', 'James Payne the new Hawtness')

    print oldFather

    And the shocking results:

      Your father is named...whatshisname...

      Your father is named...James Payne the new Hawtness...

    More Python Articles
    More By James Payne


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

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