Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact

Well House Consultants
You are on the site of Well House Consultants who provide Open Source Training Courses and business hotel accommodation. You are welcome to browse and use our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
Python module Y108
String Handling
Exercises, examples and other material relating to training module Y108. This topic is presented on public courses Learning to program in Python, Python Programming, Intermediate Python

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.

Related technical and longer articles
Analysing incoming data lines

Articles and tips on this subjectupdated
4213Formatting 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 ...
2013-11-19
 
4152Why 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 ...
2013-08-24
 
4027Collections 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 ...
2013-03-04
 
3886Formatting 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 ...
2012-10-13
 
3796Backquote, 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 ...
2012-07-14
 
3469Teaching 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 ...
2011-10-08
 
3468Python 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 ...
2011-10-08
 
3349Formatting 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   print ...
2011-07-07
 
3218Matching 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. OK ...
2011-03-28
 
463Splitting 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 ...
2011-03-01
 
1110Python - 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 ...
2011-01-29
 
3090Matching 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 ...
2010-12-17
 
496Python printf
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 ...
2010-06-19
 
2814Python - 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: days ...
2010-06-16
 
2780Formatted 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 0.0588235294118 Here it is, formatted to 4 figures after the decimal place: >>> ...
2010-05-25
 
2721Regular 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. ...
2010-05-16
 
2765Running 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] Starting with: from subprocess import * I can run a process and allow ...
2010-05-15
 
2692Flexible 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 ...
2010-03-25
 
2406Pound 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. ...
2009-09-15
 
2284Strings 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 ...
2009-07-12
 
1876Python Regular Expressions
Python supports string pattern matching to regular expressions, using Perl style regular expressions. The re module - loaded via   import re brings the appropriate elements into your program ... ready to use. Patterns that you want to match against are called Regular Expressions and are created ...
2008-11-08
 
1608Underlining 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 ...
2008-04-12
 
1517Python - 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 ...
2008-01-25
 
1195Regular 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). It's ...
2007-05-21
 
970String 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, ...
2006-12-09
(short)
954Splitting 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 ...
2006-11-29
 
943Matching 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. For ...
2006-11-26
 
903Pieces 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 ...
2006-11-01
 
773Breaking bread
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" ...
2006-06-22
 
560The 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 ...
2006-06-09
 
324The 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 ...
2006-06-05
 
Examples from our training material
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
Pictures
Training in Python at your office
Background information
Some modules are available for download as a sample of our material or under an Open Training Notes License for free download from http://www.training-notes.co.uk.
Topics covered in this module
Defining strings.
Raw strings.
Strings as sequences.
Strings methods.
Finding available methods, and documentation.
Splitting and joining strings.
Finding out what methods are available.
Formatting strings in Python.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.

Well House Consultants specialise in training courses in Python, Perl, PHP, and MySQL. We run Private Courses throughout the UK (and beyond for longer courses), and Public Courses 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.


You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/Y108.html • PAGE BUILT: Wed Mar 6 10:39:20 2013 • BUILD SYSTEM: wizard