Exercises, examples and other material relating to training module Y108. This topic is presented on public courses
With scarcely an exception, every program involves that manipulation of strings of text. Python has strong string handling facilities. Not only can you treat strings as sequences, but you can also run a wide variety of standard methods on them. Python supports regular expressions similar to those supported by Perl.
|Articles and tips on this subject||updated|
|4213||Formatting options in Python|
Take a look at this program run:
munchkin:n13py grahamellis$ python twoforms
There is a large piece of timber in the back garden
and the dog was sniffing around it earlier. It looks like it was left
there by the storm that came our way last ...
|4152||Why are bus fares so high? |
When Lisa and I took the bus into Devizes in the evening a couple of weeks ago, we spent around 12 pounds for the return trip, and it would have been much more convenient and cheaper for us to drive. We live less than 6 miles from Devizes, and the bus journeys at the time we went are subsidised. If the ...
|4027||Collections in Python - list tuple dict and string.|
All the languages that we teach have "collection" variables - single names under which a series of values is stored, keyed or indexed in some way. There are four such types in Python
Lists ... starting off with index position 0, and alterable within the stucture as the program runs. You may erroneously ...
|3886||Formatting output - why we need to, and first Python example|
I understand that if you win the lottery in a big way, a lot of long lost relatives who have fallen on hard times will come out of the woodwork ... and if you've got a lot of brothers and sisters you might find a surprising number of nieces and nephews!
Being a generous soul, you've decided to split ...
|3796||Backquote, backtic, str and repr in Python - conversion object to string|
The backquote or backtic operator in Python is a pseudonym for the repr function.
If you have an object that you want to convert into a string (to manipulate it, store it, print it out), Python may call one of two standard method:
__str__ (also the str function) converts an object into a human-readable ...
|3469||Teaching dilemma - old tricks and techniques, or recent enhancements?|
Where there's something that's a frequent requirement on one of the subjects we teach, but can be hard to achieve, we'll spend more than just a minute or two covering it on our courses. After all, the tips and techniques of how to make the most of a programming language are every bit as important as ...
|3468||Python string formatting - the move from % to str.format|
The % operator in Python has been a very clever and easy-to-use formatter, but has lacked the extensibility that's been incumbent on its structure and mirroring of the C sprintf function. Or - put another way - it was rather irritatingly running out of steam for some of the more complex formatting ...
|3349||Formatting output in Python through str.format|
Python's str.format method provides functionallity to convert values into string representations - typically for output. Taking (for example) the resukt or dividing 1 by 7 - a recurring decimal - and rounding and truncating in to 2 decimal places if it's a price ...
value = 1.0 / 7.0
|3218||Matching a license plate or product code - Regular Expressions|
Questions from my mailbox:
1. Regular expression to accept the following form of license plate numbers. Three letters followed by between 1 and 4 digits.
2. A regular expression to accept the following product codes: Between 1 and three characters (in capitals) followed by between one and three digits.
|463||Splitting the difference|
Perl's split function takes a string of text, and divides it up at a delimiter of your choice into a list of shorter strings ... it's one of the "power tool" functions of Perl and a vital part of the language. So how come that you can write a Tcl program and use its version of split - or omit the split ...
|1110||Python - two different splits|
In Python, there are two different split methods you can use to break up a string into a number of substrings, based on a particular separator. If you know exactly what character(s) your separator will be - e.g. exactly one space - the you can use the method in the string class. By if your separator ...
|3090||Matching to a string - what if it matches in many possible ways?|
If you're looking to match and capture part of a string that matches your pattern, you have to be very careful to ensure that you match the correct part of the incoming string. If - for example - I were to ask you what 3 digit numbers the text "I live at 404 and my phone number is 708225" contains, you ...
To format data into a string in python, use the % operator as python doesn't have an sprintf or printf function. So why have I entitled today's entry "python printf? Because this in one of the most frequently asked, and infrequently answered questions.
The % operator with a string to its left formats ...
|2814||Python - splitting and joining strings|
In Python, you can join a list (or a tuple) of string objects into a single string object, using the join method in the string class. As the join method's in the string class, it has to be called on a string object, and that's the separator that you're putting between each of the joined elements. Thus:
|2780||Formatted Printing in Python|
Python has no "printf" or "sprintf" - use the % operator on a string object which is the format string instead. Let me show you that ...
Firstly, what do we get if we do NOT format?
>>> val = 1. / 17.
>>> print val
Here it is, formatted to 4 figures after the decimal place:
|2721||Regular Expressions in Python|
I took advantage of the lack of a whiteboard yesterday to write notes on the screen - in an edit window - while I was teaching a class about regular expressions, and how they're used in Python. Which has resulted in a rather nice example that's now online [here].
Regular expressions can frighten newcomers. ...
|2765||Running operating system commands from your Python program|
As from Python 2.6, os.popen and friends are deprecated methods and you should use the subprocess module for subprocesses. So that's the way to go if you want to run operating system commands. New example showing this - [here]
from subprocess import *
I can run a process and allow ...
|2692||Flexible search and replace in Python|
There are various different ways you may want to search and replace within a string - and this post shows you how a lot of them work in Python.
1. You may want to replace one literal piece of text by another
2. You may want to replace a string matching a pattern by a literal string
3. You may want ...
|2406||Pound Sign in Python Program|
How do I get a pound sign up in Python? A regular question ans the regular answer is to use a unicode string:
>>> saying = u'It will cost \u00a310.00'
>>> print saying
It will cost £10.00
Which is all well and good, but people want to be able to type the pound sign into the source code too. ...
|2284||Strings as collections in Python|
In Python, I can treat a string as a collection of characters and iterate through it without the need to do any sort of conversion on it, or muck about with "substr" ...
breakfast = "Croissants and toast"
for letter in breakfast:
print "Give me a ",letter
print "and we ...
|1876||Python Regular Expressions|
Python supports string pattern matching to regular expressions, using Perl style regular expressions.
The re module - loaded via
brings the appropriate elements into your program ... ready to use.
Patterns that you want to match against are called Regular Expressions and are created ...
|1608||Underlining in Perl and Python - the x and * operator in use|
Perl's x operator - yes, that is the letter x - is used to replicate the string on the left the number of times given to the right. "What use is THAT" I have been asked in the past, by delegates feeling that it's a solution looking for a problem. Well - as an example, it's a great way to output just ...
|1517||Python - formatting objects|
If you're going to be printing out objects from within Python, simply provide an __str__ method in the class and it will do all the work for you. Indeed - why not create classes and objects for straightforward objects such as people's names ... then you can call up the formatter for them very easily ...
|1195||Regular Express Primer|
Over the years I've been teaching people about Regular Expressions, I've learnt what does and doesn't work in such tuition. A casual question I saw yesterday got me writing, and I've just posted up a new technical article to the solution centre - see here (it will open in a separate window).
|970||String duplication - x in Perl, * in Python and Ruby|
In Python and Ruby you can duplicate a string by running the multiply operator (*) on a string object, and in perl you can use the x operator. There have been many times when I've looked at this facility and said to myself "very nice, but isn't that a feature looking for a benefit" - in other words, ...
|954||Splitting Pythons in Bradford|
Python supports two split functions methods to explode a string into a list of substrings. There's one in the default class that works on a string, and there's another in the re (Regular Expression) class.
Splitting at a string works very will if you've got a fixed delimiter, typically in a machine ...
|943||Matching within multiline strings, and ignoring case in regular expressions|
Regular Expressions are powerful matching tools and you can specify almost anything within them. But there are certain facilities that are naturally applied to the regular expression as a whole rather than to parts of the match, and there are specified in a different way in each language / implementation.
|903||Pieces of Python|
From a most interesting Saturday which was spent doing a one on one session on thread, wxPython, etc - some Python snippets that provide unusual demos and hard-to-find answers:
The Backtic operator evaluates an expression and returns the result as a string
val1 = 16
val2 = 18
result = "The result ...
How can I take ONE thing, discard some of it, and end up with MANY things all of the same type as the original? Surely that doesn't make sense does it?
I could take a packet of half a dozen rolls from the supermarket, made (as they often are in the UK) as a single piece of bread with narrow "tear" ...
|560||The fencepost problem|
If you erect a fence of 10 panels, you'll need 11 fenceposts to hold it up. And if you write a program that joins together 10 elements on a line, you'll only need 9 separators between them - this is known as the "fencepost problem".
If you write a simple program loop to output each element from a list ...
|324||The backtick operator in Python and Perl|
In Perl, and in shell programs too, the backtick operator causes the enclosed string to be executed as a command and the result (as generated on STDOUT) returned.
In Python, it's different. The backtick operator causes the enclosed expression to be evaluated and returned as a string.
See string handling ...
|Pythoners|| Parse a file checking each line against a criterion|
|backtick|| Conversion to a string with the backtick operator|
|bejo.py|| split and join|
|chunky|| Splitting at a literal v splitting at a regex|
|cli.py|| Command line handler function in Python|
|cli_withopts.py|| Command line handler function in Python|
|clinput|| reading and handling command line options|
|cori|| Multiline string|
|fares.py|| Distortion of bus prices due to concessionary fares|
|fence.py|| Fencepost problem and solution|
|fhand|| Unicode text string |
|funsub|| Substitute for a regular expression|
|gift|| Share 10000 pound between 1 to 10 children|
|grab|| Regular Expression - Maya strings|
|inflate|| Search for regular expression and replace |
|irish_grid|| Ordnance Survey Ireland - Grid Reference Conversion|
|kid|| Formatting strings / multiple use of % operator|
|listmethods|| List all methods available on an object|
|lit|| Checking the type of characters entered|
|mypi|| Strings, raw strings, triple quoted strings|
|nf|| Formatting - time and money using the % operator|
|p3.py|| raw and regular strings|
|pyform|| Python Formatting via the str.format method|
|pys|| Strings - how to write them|
|qre|| Split - regular expression v literal string|
|regex|| Search and find, replace, and replace with result|
|reggie.py|| Regular Expression Overview|
|reginald|| Regular expression with comments in it|
|rexy|| writing understandable regular expressions|
|seq2.py|| Using the replace method on a string|
|seqrev.py|| translate, reverse and join - reversing dna sequence|
|sequence.py|| Handling a string as a list of characters|
|splitcare|| Comparison of regex and string splitting|
|strf|| The String formatter |
|taxform.py|| Formatting output - floats, ints, etc|
|tliser|| Exercise answer - formatting prices and aligning columns|
|twoforms|| Two forms of formatting|
|visitors|| Web site - population penetration|
|young.py|| revision - string basics|
Strings as sequences.
Finding available methods, and documentation.
Splitting and joining strings.
Finding out what methods are available.
Formatting strings in Python.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.