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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Python Strings: Spinning Yarns
    (Page 1 of 4 )

    As the students used to say to Mr. Kotter: "Welcome back, welcome back, welcome back." In our previous article we talked some more about how to manipulate strings in Python, leaving off with indexing and slicing. Here, we will pick up again with slicing, using it to “change” the contents of a string.I say "change" in quotation marks because technically strings are immutable, or in less fancy speak, they are like a hippy's views of the world -- they cannot be changed. Unless of course the 1980s and big business rolls around and those hippies discover the wonderful world of free commerce. You get a job you damn hippy!

    Just a side note: hippy is not in my Word dictionary, but Lippy, Tippy, Pippy, and Hippo are for some reason.

    Tricking the Computer

    I don't know why we have to trick the computer in order to change a string. I mean, why couldn't strings just be changeable? Because computer programmers like to mess with you, that's why, especially the ones that create languages and specifically the ones that create open source languages. They don't make money, so their sole satisfaction is to confound you. And let's face it, that isn't too hard to do.

    Here is how we "change" those hippy strings. Note that we will be writing this code at the command prompt of our Python shell.


    >>> woodstock='I am a hippy'

    >>> wallstreet=woodstock[0:7] + 'yuppie'

    >>> print wallstreet

    I am a yuppie

    Let's have a little more fun with it. In the following example, we will assign the alphabet to a variable named, well, alphabet, then use slicing to print out the coolest name in history. Note that the index number for the string begins with 0, so for instance, a is index 0, b is in index 1, and z is index 25, as opposed to 26.


    >>> alphabet='abcdefghijklmnopqrstuvwxyz'

    >>> name=alphabet[9]+alphabet[0]+alphabet[12]+alphabet[4]+alphabet[18]

    >>> print name

    james

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