Python
  Home arrow Python arrow Page 2 - Python Strings: Spinning Yarns
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 Strings: Spinning Yarns
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2008-02-11

    Table of Contents:
  • Python Strings: Spinning Yarns
  • The Interpolation Operator
  • Changing Strings with Methods
  • Dealing with Multiple Words

  • 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Python Strings: Spinning Yarns - The Interpolation Operator
    (Page 2 of 4 )

    When we wish to specify what happens to a value when we insert it into a string, we can do so using the interpolation operator(%). We can use this to insert data inside of a string, determine how many digits are displayed next to a decimal, determine how much space to allow for the data (known as the minimum field width), and more.

    Here is a list of possible formatting code you can insert after the interpolation operator(%):


    • %c - used for a single character

    • %d and %i - used for signed integer decimals

    • %f and %F - used for floating point decimals

    • %r - represents the data

    • %% - used as an escape character for the percentage symbol

    To insert a single character into a string, we can do so, as described above, using %c. Here it is in code:

    missing='F'

    print 'Which character is missing from the following sequence?'

    print 'A B C D E %c G H I J K L M N O P Q R S T U V W X Y Z' %missing

    When the code prints, it replaces the code %c with the value in our variable, missing, which in this instance is 'F'. Here is the result:

      Which character is missing from the following sequence?

      A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

    If we wanted to insert an entire word or sentence, or string, we could do that as well by using %s:


    hawtness='James Payne'

    print "And the winner of this years Uber Hawtness award goes to....*drumroll*"

    print "The magnanimous, heroic, super-strong, giant-brained, whicka whicka %s ! Whoeee!" %hawtness

    In this code, the program sees the %s and replaces it with the value stored in the variable hawtness. The result is:

      And the winner of this years Uber Hawtness award goes to....*drumroll*

      The magnanimous, heroic, super-strong, giant-brained, whicka whicka James   Payne ! Whoeee!

    We can, of course, also insert multiple words into our string, using the following method:


    hawtness='James Payne'

    gf='Angelina Jolie'

    name='Jamesalina'

    print 'Here comes the hot new hollywood couple now.'

    print '%s in an Armani suit, looking dashing and %s in a birthday suit as %s demands it!' % (hawtness, gf, hawtness)

    print 'As a couple we dub thee...%s' %name

    This displays:

      Here comes the hot new hollywood couple now.

      James Payne in an Armani suit, looking dashing and Angelina Jolie in a birthday suit as James Payne demands it!

      As a couple we dub thee...Jamesalina

    The technique works for characters as well:


    first='a'

    second='b'

    third='c'

    fourth='d'

    fifth='e'

    sixth='f'

    seventh='g'

    eighth='h'

    print 'Here are the first eight letters of the alphabet...'

    print '%c %c %c %c %c %c %c %c' % (first, second, third, fourth, fifth, sixth, seventh, eighth)

    And the result:

      Here are the first eight letters of the alphabet...

      a b c d e f g h

    More Python Articles
    More By James Payne


       · Thanks for stopping by to read my article on Python Strings, where we go a little...
     

       

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