Python
  Home arrow Python arrow Page 3 - 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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

String Manipulation
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 62
    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


    String Manipulation - Numbers and spaces


    (Page 3 of 4 )

    We can check whether a string is alphanumeric:

    >>> 'aa44'.isalnum()
    True
    >>> 'a$44'.isalnum()
    False

    It is also possible to check whether a string contains only letters:

    >>> 'letters'.isalpha()
    True
    >>> 'letters4'.isalpha()
    False

    Here's how you check whether a string contains only numbers:

    >>> '306090'.isdigit()
    True
    >>> '30-60-90 Triangle'.isdigit()
    False

    We can also check whether a string only contains spaces:

    >>> '   '.isspace()
    True
    >>> ''.isspace()
    False

    Speaking of spaces, we can add spaces on either side of a string. Let's add spaces to the right of a string:

    >>> 'A string.'.ljust ( 15 )
    'A string.      '

    To add spaces to the left of a string, the rjust method is used:

    >>> 'A string.'.rjust ( 15 )
    '      A string.'

    The center method is used to center a string in spaces:

    >>> 'A string.'.center ( 15 )
    '   A string.   '

    We can strip spaces on either side of a string:

    >>> 'String.'.rjust ( 15 ).strip()
    'String.'
    >>> 'String.'.ljust ( 15 ).rstrip()
    'String.'

    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...
       · help me how to create this program!create a program that will input a letter...
       · Hi there,Great article. It helped and will help a lot in the future.However I...
     

       

    PYTHON ARTICLES

    - 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
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT