Python
  Home arrow Python arrow Page 3 - Python Strings: Spinning Yarns
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? 
Google.com  
PYTHON

Python Strings: Spinning Yarns
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    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:
      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 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
     

       

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek