Python
  Home arrow Python arrow String Manipulation
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 
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 / 57
    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
    (Page 1 of 4 )

    Programmers need to know how to manipulate strings for a variety of purposes, regardless of the programming language they are working in. This article will explain the various methods used to manipulate strings in Python.Introduction

    String manipulation is very useful and very widely used in every language. Often, programmers are required to break down strings and examine them closely. For example, in my articles on IRC (http://www.devshed.com/c/a/Python/Python-and-IRC/ and http://www.devshed.com/c/a/Python/Basic-IRC-Tasks/), I used the split method to break down commands to make working with them easier. In my article on sockets (http://www.devshed.com/c/a/Python/Sockets-in-Python/), I used regular expressions to look through a website and extract a currency exchange rate.

    This article will take a look at the various methods of manipulating strings, covering things from basic methods to regular expressions in Python. String manipulation is a skill that every Python programmer should be familiar with.

    String Methods

    The most basic way to manipulate strings is through the methods that are build into them. We can perform a limited number of tasks to strings through these methods. Open up the Python interactive interpreter. Let's create a string and play around with it a bit.

    >>> test = 'This is just a simple string.'

    Let's take a fast detour and use the len function. It can be used to find the length of a string. I'm not sure why it's a function rather than a method, but that's a whole nother issue:

    >>> len ( test )
    29

    All right, now let's get back to those methods I was talking about. Let's take our string and replace a word using the replace method:

    >>> test = test.replace ( 'simple', 'short' )
    >>> testa
    'This is just a short string.'

    Now let's count the number of times a given word, or, in this case, character, appears in a string:

    >>> test.count ( 'r' )
    2

    We can find characters or words, too:

    >>> test.find ( 'r' )
    18
    >>> test [ 18 ]
    'r'

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

     
    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