SunQuest
 
       Python
  Home arrow Python arrow Page 2 - String Manipulation
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 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

String Manipulation
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 59
    2005-05-02

    Table of Contents:
  • String Manipulation
  • Splitting strings, making cases
  • Numbers and spaces
  • Regular Expressions

  • 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

    String Manipulation - Splitting strings, making cases


    (Page 2 of 4 )

    Splitting a string is something I find myself doing often. The split method is used for this:

    >>> test.split()
    ['This', 'is', 'just', 'a', 'short', 'string.']

    We can choose the point that we split it at:

    >>> test.split ( 'a' )
    ['This is just ', ' short string.']

    Rejoining our split string can be done using the join method:

    >>> ' some '.join ( test.split ( 'a' ) )
    'This is just  some  short string.'

    We can play around with the case of letters in our string, too. Let's make it all upper case:

    >>> test.upper()
    'THIS IS JUST A SHORT STRING.'

    Now let's make it lowercase:

    >>> test.lower()
    'this is just a short string.'

    Let's capitalize only the first letter of the lowercase string:

    >>> test.lower().capitalize()
    'This is just a short string.'

    We can also use the title method. This capitalizes the first letter in each word:

    >>> test.title()
    'This Is Just A Short String.'

    Trading case is possible:

    >>> test.swapcase()
    'tHIS IS JUST A SHORT STRING.'

    We can run a number of tests on strings using a few methods. Let's check to see whether a given string is all upper case:

    >>> 'UPPER'.isupper()
    True
    >>> 'UpPEr'.isupper()
    False

    Likewise, we can check to see whether a string contains only lower case characters:

    >>> 'lower'.islower()
    True
    >>> 'Lower'.islower()
    False

    Checking whether a string looks like a title is simple, too:

    >>> 'This Is A Title'.istitle()
    True
    >>> 'This is A title'.istitle()
    False

    More Python Articles
    More By Peyton McCullough


       · You should probably offer at least a line or two of explanation as to <em>why</em>...
       · I agree, the article should provide a better reason for using string methods rather...
       · I was reading your article because i am looking for a way to pick up a character by...
       · People could learn Python, a wonderful object-oriented language, but moron students...
       · This Article was helpful but I am interested in being able to pull data from a...
       · Try looking at the split method:>>> myParameters = "a=1,b=2,c=3,d=4">>>...
       · i want to word by wordlike example given belowa="hi i am ...
       · The split method will do what you want:>>> a = "some words here">>>...
       · Python's equivalent for Mid$(start, stop) would be someString[start:stop]. Don't use...
     

       

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