Python
  Home arrow Python arrow Page 3 - Python: More Fun with Strings
CIO Insight
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: More Fun with Strings
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    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:
      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: More Fun with Strings - Capital Idea(s)
    (Page 3 of 5 )

    We can also use methods to control the appearance of strings, as in the following examples. In our first code, we will make the first letter of our string uppercase, using the capitalize method():


    >>> book.capitalize()

    'It was a dark and stormy night, the wind blowing through the trees, and somewhere, out there in the far off teinkling of city lights, britney spears was busy being a bad mother...'

    You will note that nothing really changed since our text was already capitalized to begin with.

    In this next example we will use the title method() to capitalize the first letter of every word:


    >>> book.title()

    'It Was A Dark And Stormy Night, The Wind Blowing Through The Trees, And Somewhere, Out There In The Far Off Teinkling Of City Lights, Britney Spears Was Busy Being A Bad Mother...'

    And to make every letter uppercase, we use the upper method():


    >>> book.upper()

    'IT WAS A DARK AND STORMY NIGHT, THE WIND BLOWING THROUGH THE TREES, AND SOMEWHERE, OUT THERE IN THE FAR OFF TEINKLING OF CITY LIGHTS, BRITNEY SPEARS WAS BUSY BEING A BAD MOTHER...'

    I know, I know...hold back all your Britney is a Good Person YouTube videos. We're almost done.

    And of course we can also change everything to lowercase, using the lower() method:


    >>> book.lower()

    'it was a dark and stormy night, the wind blowing through the trees, and somewhere, out there in the far off teinkling of city lights, britney spears was busy being a bad mother...'

    Also, you can switch between upper and lower using the swapcase() method. In or previous example, everything was changed to lower case. We can now use swapcase to switch back to uppercase. If we wanted to switch back to lowercase after that, we could again use switchase:


    >>> book.swapcase()

    'iT WAS A DARK AND STORMY NIGHT, THE WIND BLOWING THROUGH THE TREES, AND SOMEWHERE, OUT THERE IN THE FAR OFF TEINKLING OF CITY LIGHTS, bRITNEY sPEARS WAS BUSY BEING A BAD MOTHER...'

    As I said before, these are only a few of the string methods available; we will discuss the rest at a later date.

    More Python Articles
    More By James Payne


       · Hey thanks for stopping by for our continued discussion on String manipulation in...
     

       

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