Design

Showing posts with label string formatting. Show all posts
Showing posts with label string formatting. Show all posts

Tuesday, November 2, 2010

' \ ' Escapes in Python

I have previously mentioned that '\n' codes for a new line. The backslash is known in Python as an escape.
>>> print "\tThis is a tabbed line"
This is a tabbed line
>>> print "This sentence \nis split on \nthree lines"
This sentence
is split on
three lines
>>> print "Type \\ for a backslash"
Type \ for a backslash
>>> print "Verticle \vTab"
Verticle
Tab
>>> print "Shopping List: \vPrenatal vitamins \vCocoa Butter \
... \vOrange juice"
Shopping List:
Prenatal vitamins
Cocoa Butter
Orange juice
>>> print "Shopping List: \n\t*Prenatal vitamins \
...\n\t*Cocoa Butter \n\t*Orange juice"
Shopping List:
*Prenatal vitamins
*Cocoa Butter
*Orange juice
>>> print 'Prevent the single quote 5\'5" from ending the string'
Prevent the single quote 5'5" from ending the string
>>> print "Prevent the double quote 5'5\" from ending the string"
Prevent the double quote 5'5" from ending the string

Notice that I also used a backslash in my string so that I could continue onto the next ine without Python thinking my string was done. There are other escapes in Python you can read about. "Carriage return" is a term from type writers to mean that you move to the beginning of a line. Linefeed and formfeed are also typewriter terms. \n and \t will be the most frequently used, though you may also want to be familiar with other escapes for unique problems.

Sunday, October 24, 2010

String Formatting in Python

An easy way to combine strings with non strings is to use commas:
>>>>>> print 'The value of pi is often rounded to',3.14
The value of pi is often rounded to 3.14

But what if you want to create a website that welcomes guests by their name after he or she logs in? What if you are pulling information from a list or a dictionary? For personalization or multiple use functionality, we will be using string formatting. Below are the different format characters. There are a few others, but they aren't necessary.
%s   string
%d integers or decimals, but returns floor integer
%r anything that is not a string, converts object to a string
%f floating point, so we can control precision after decimal

Here is an example of using each of the formats:
>>> name='Anderson Silva'
>>> weight= 105 #integer
>>> height= 1.92 #decimal, but I only want 1.9 displayed
>>> home= 'Brazil' #string, watch what happens with %r
>>> fighter1='''%s
... Nationality: %r
... Weight: %d kg
... Height %.1f m''' % (name,home,weight,height) #in order
>>> print fighter1
Anderson Silva
Nationality: 'Brazil' #%r leaves quotes around strings
Weight: 105 kg
Height 1.9 m

Here is an example converting centimetres to inches
>>> def convert(cm):
... inches=cm*.39370
... print 'There are %.2f inches in %d centimetres' % (inches,cm)
...
>>> convert(10)
There are 3.94 inches in 10 centimetres
>>>#two places after decimal are shown
>>> convert(3.544445)
There are 1.40 inches in 3 centimetres

%f has a default to print six numbers after the decimal. By adding '.1', '.2', '.3', etc... between the '%' and 'f', you determine how many numbers are visible after the decimal. You can also add '+' or '-' before the '.x', such as showing a change
>>> print "Today's stock price changed %+.2f" % 1.4888
Today's stock price changed +1.49