Personal tools
You are here: Home → Comp 171 Scripting Practicum → Course Materials → Python Materials → Python Programs → Python String Formatting
Document Actions

Python String Formatting

by Dr. William L. Honig — last modified 2012-03-13 10:47 AM

Using Pythons str.format() to give whatever form you want to text

Click here to get the file

Size 3.4 kB - File type text/python-source

File contents

# 
#Comp 171 Standard Python Script Header 
#Assignment name: String Formatting Examples
#LUC id and Name whonig@luc.edu - Dr. William L. Honig 
#Date 7 March 2012 
# 
#Examples of the Python String Format method
#For documentation and more details see
#  http://docs.python.org/dev/py3k/library/string.html
#  especially sections 6.1.3, 6.1.3.1, and 6.1.3.2
# 
#Questions: Can you figure out some additional things to try? 
# 
#
def startingFormats():
    aString = "The replacement {0} have a positional {1} in curly brackets starting with {2}"
    bString = aString.format("fields", "number", 0)
    #note that aString is unchanged.  str.format produces a new string value you can use or store in a variable
    print(aString, bString, sep="\n")

    #if you have a list of things you want to format...
    aList = ["fields", "number", 0]
    print( tuple(aList) )
    #cString = aString.format( tuple(aList) )  #does not work!
    cString = aString.format( aList[0], aList[1], aList[2] )
    print( cString )

    #an example: positional parameter 0 is replaced by the value given
    #   and formatted with fill character 0, alignment =, sign +, width 10
    dString = "{0:0=+10}"
    print( dString.format( 9 ) )
    
def exampleFormats():
    #str.format takes any number of parameters that are evaluated
    #  and matched with the replacement fields in the str being used
    #
    #each replacement field can include many format choices and options
    #  the general structure of the format specification is
    #  [[fill]align][sign][#][0][width][,][.precision][type]
    #
    #width format - gives a minimum width.  Otherwise adjust to size of value
    aNumber = "{0}"
    bNumber = "{0:15}"
    print(aNumber.format(12345.6789), bNumber.format(12345.6789), sep="\n")
    #try it with strings too
    #
    #using , as a separator and specifying precision for floating point number type
    cNumber = "{0:15,f}"
    dNumber = "{0:15,.8f}"
    eNumber = "{0:6,.3f}"  #longer than the minimum
    print(cNumber.format(12345.6789), dNumber.format(12345.6789),eNumber.format(12345.6789), sep="\n")
    #Try it using some other numbers, maybe taking input from the user and then print with a format
    #
    #percentage format with and without precision specified
    fNumber = "{0:%} is the same as {0:.2%}"
    gNumber = "{0:%} is the almost the same as {0:.2%}"
    print( fNumber.format(1) )
    print( gNumber.format(12/45) )
    #
    #alignment can be left, right, centered within the width
    #  and a special = alignment for numbers
    hNumber = "{0:0=+10}"
    iNumber = "{0:<10}"
    jNumber = "{0:^10}"
    kNumber = "{0:>10}"
    print(hNumber.format(99), iNumber.format(99), jNumber.format(99), kNumber.format(99), sep="\n")
    #
    #zero padding can be added to fill the whole width
    lNumber = "{0:010}"
    print(lNumber.format(123))
    
def moreFormats():
    #working with text strings
    #
    #fill format - any character you want except { }
    aString = "{0:#>}"  #no effect unless string length < minimum length
    bString = "{0:#>30}"
    print(aString.format("How Long Is This String?"), bString.format("How Long Is This String?"), sep="\n")
    #
    #precision for text gives the *maximum* size
    cString = "{0:#>.15}"
    print(cString.format("A Bit Too Long!!!"))
          
          
    
    
« October 2026 »
Su Mo Tu We Th Fr Sa
123
45678910
11121314151617
18192021222324
25262728293031
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: