Python: Strings

This is part of my Python & Django Series which can be found here including information on how to download all the source code.

Basics

Strings in Python can be created with either single or double quotes.

Code:

print('single quotes')
print('double quotes')

Output:

single quotes
double quotes

What is great about having the choice is that you can swap between the two when you need a string which contains the other without having to escape any characters.

Code:

print(' " double quotes" ')
print(" ' single quotes' ")

Output:

” double quotes”
‘ single quotes’

The traditional backspace “\” can still be used to escape special characters.

Code:

print(' ' single escaped quotes ' ')
print(" " double escaped quotes " ")

Output:

‘ single escaped quotes ‘
” double escaped quotes ”

When there are many special characters which require escaping a raw string can be created by simply prefixing the string with an r. This takes the string as verbatim.

Code:

print(r"Thisisarawstring@ in c#")

Output:

This\is\a\raw\string\@ in c#

Strings, which are very long, can be split between multiple lines.

Code:

print(("This can help"
       " separate Strings"))

print("This can also help  
separate Strings")

Output:

This can help separate Strings
This can also help separate Strings

Collections Of Characters

Strings are nothing more than an immutable list of characters. A lot of the functionality applicable to collections is applicable for strings.

Code:

a_string = 'HelloWorld!'
print(type(a_string))       # The string class
print(len(a_string))        # The length
print(a_string[0])          # Char at index 0
print(a_string[2:5])        # Chars at index 2 to 5

Output:

11
H
llo

Concatenation

Strings can be concatenated with the + operator or replicated with the * operator.

Code:

a_string = 'HelloWorld!'
print(a_string + "TEST")    
print(a_string * 2)

Output:

HelloWorld!TEST
HelloWorld!HelloWorld!

The format function can also be used when concatenating strings. Here parameters are placed into template holders of a string.

The templates are defined as ordinal positions {0} or as named {name}. They are then filled in by the parameter with the same ordinal position or named. They can even be mixed in the same function call.

Code:

print('{0} can have {1} templates or placeholders'.format('Strings', 'ordinal'))
print('{first} can have {second} templates or placeholders'.format(first='Strings', second='named'))
print('{0} and {named} placeholders can be mixed!'.format('Ordinal', named='named'))

Code:

> Strings can have ordinal templates or placeholders
> Strings can have named templates or placeholders
> Ordinal and named placeholders can be mixed!

The format function also allows key value pair references to be displayed with template notation.

Code:

> data_table = {'One': 'And a one!', 'Two': 'And a two!'}
> print(type(data_table), data_table)
> print('One: {0[One]:s}; Two: {0[Two]:s}'.format(data_table))
> print('One: {One:s}; Two: {Two:s}'.format(**data_table))        # With Unpacking

Output:

<class 'dict'> {'Two': 'And a two!', 'One': 'And a one!'}
One: And a one!; Two: And a two!
One: And a one!; Two: And a two!

The older versions of Python used implicit ordinal positioning along with the output type. This functionality is also still allowed.

Code:

print("Hello there %s %s %s" % ("Mr", "Luke", "Wickstead"))

Output:

Hello there Mr Luke Wickstead

The %s is a string format option; there are many parameters available for formatting types to strings. See the section “Format Specification Mini-Language” bellow for more options.

Padding

Strings can be padded with with rjust ljust and center functions. These functions adds spaces to the left, right or each side respectively, until the string contains the defined number of characters. The zfill function is the same as rjust though 0’s are used to pad the string instead of spaces..

Code:

print("1".rjust(3))
print("1".ljust(3))
print("1".center(3))
print("1".zfill(3))  # zeros to left

Output:

” 1″
“1 “
” 1 ”
“001”

Format Specification Mini-Language

Overview

We have already looked at the format function for concatenating strings. Along with the ordinal or named element of the data we can also provide other criteria which will affect how the strings is formatted. The full possible format for a template parameter is as follows:

Spec:

|[[fill]align][sign][#][0][width][,][.precision][type]

Possible Values of the spec elements.

Name Possible Description
Filll [Any Char] Any char to be used as a fill char
Align = ^ Fill alignment. Left, right, left sign aware and centre
Sign +,-, ” “ Sign always on shown, only for negative, space for +ve and – for -ve
# # Formats binary, octal or hex numbers prefixed with ‘0b’, ‘0o’, or ‘0x’
0 0 Use sign aware 0 padding when turning on the width
Width Integer Turn on 0 padding for numbers
, , Use commas to separate number rages
Precision Integer Round with x number of decimal places
Type b, c, d, e, E, f, F, g,G,n,o,s,z Defines what format type the data is to be represented.

The type reference equates to the following:

Type Name Description
s String
b Binary
c Character
d Decimal ( base 10 ) integer
o Octal
x X Hex
n General number which will use the computers localisation settings to format the number.
d None type
e E Exponential number
g G General provides general rules for precision, rounding and when a max number before switching to exponential
% ercentage

We now look at some examples for common situations. More information can be found here

Formatting Floats

[+- ][0][width][,][.precision]  
[Sign Setting][Use 0 Instead of Space For Padding][Use Comma As Separator][Final Width In characters]

Code:

print("{0:.0f}".format(123.456))      # 0 dp
print("{0:.2f}".format(123.456))      # 2 dp
print("{0: 08,.3f}".format(123.456))  # 3 dp with padding to 8 chars
print("{0:+.3f}".format(123.456))         # 3 dp with +/-
print("{0:-.3f}".format(-123.456))        # 3 dp with - if -ve.
print("{0:,}".format(-1121212123.456))  # , (comma) separator

Output:

123
123.46
” 123.456″
+123.456
-123.456
-1,121,212,123.456

If the entity is a percentage we can round and append on a % sign as followed.

Code:

print("{0:.3%}".format(0.25555))    # Format percentage to 3dp

Output:

25.555%

Space Padding

[*][<>=^][int]
[Pad Char][Alignment Type][Final Width In Chars]

Code:

print("{0:0>5d}".format(1))     # Left pad with 0 to 5 chars
print("{0:0<5d}".format(1))     # Right pad with 0 to 5 chars
print("{0:0^5d}".format(1))     # Centre pad with 0 to 5 chars
print("{0:5d}".format(1))       # Left pad with space to 5 chars
print("{0:<5d}".format(1))      # Right pad with space to 5 chars
print("{0:^5d}".format(1))      # Centre pad with space to 5 chars

Output:

“00001”
“10000”
“00100”
” 1″
“1 “
” 1 ”

Types

The last parameter is always the type. Each type can then be configured using the above settings.

Some examples of how to output various types to various output representation types.

Code:

print("{0:g}".format(1111123.456))      # As general
print("{0:5.2n}".format(123.456))       # As number
print("{0:b}".format(123))              # As Binary
print("{0:x}".format(123))              # As hex
print("{0:e}".format(123))              # As exponential

Output:

1.11112e+06
1.2e+02
1111011
7b
1.230000e+02

The world of possibilities are endless. It is not really possible to be exhaustive but hopefully this is enough to get people started.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s