Python
  Home arrow Python arrow Page 4 - Python 101 (part 2): If Wishes Were Pythons
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

Python 101 (part 2): If Wishes Were Pythons
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2001-05-31


    Table of Contents:
  • Python 101 (part 2): If Wishes Were Pythons
  • Tax Evasion
  • Q
  • Sliced And Diced
  • Comparing Apples And Oranges
  • If Only...
  • Tying Up The Loose Ends
  • Cookie-Cutter Code
  • Time For Lunch

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Python 101 (part 2): If Wishes Were Pythons - Sliced And Diced
    ( Page 4 of 9 )

    In Python-lingo, strings are an "immutable" object type, which means that they cannot be changed in place; the only way to alter a string is to create a new string with the changes. Python comes with powerful built-in operators designed to make it easier to extract subsections of a string, and thereby create new strings.

    For example, to extract the letter "b" from the string "hobgoblin", I could use

    >>> str = "hobgoblin" >>> str[2] 'b' >>>
    This is referred to as "slicing"; the square braces [ and ] specify the starting and ending location (or "index") for the slice. Note that the first character is referred to by index 0.

    You can extract a substring by specifying two indices within the square braces...

    >>> str = "hobgoblin" >>> str[3:9] 'goblin' >>>
    ...and watch what happens when I omit either one of the two indices.

    >>> str = "hobgoblin" >>> str[3:] 'goblin' >>> str[:7] 'hobgobl' >>>
    You can also use negative indices, to force Python to begin counting from the right instead of the left.

    >>> str = "hobgoblin" >>> str[-6] 'g' >>> str[-6:] 'goblin' >>>
    The built-in len() function can be used to calculate the number of characters in a string,

    >>> str = "hobgoblin" >>> len(str) 9 >>>
    while the "in" and "not in" operators can be used to test for the presence of a particular character in a string. A match returns 1 (true), while a failure returns 0 (false).

    >>> str = "hobgoblin" >>> "g" in str 1 >>> "x" in str 0


     
     
    >>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek