Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))

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
4659Prining a pound sign from Python AND running from the command line at the same time
In order to make a python program executable on a Linix or Unix (e.g. OSX) system, you should:   a) change the file permissions using chmod (e.g. chmod +x poundland )   b) Add as the very first line of the file #!/usr/bin/env python so the operating system knows it's a python program   c) ...
4595Python formatting update - including named completions
As from Python 2.6, a format method has been introduced in addtion to the % operator on strings to provide a more flexible and improved way of convering objects (of various type) into strings. Select [here] to open the source in a new window which was used to get this result:   munchkin:cambx grahamellis$ python3 earlier    How all are ya (in metres) 1.76   How heavy are ya (in kgs) 74   Your bmi is 23.889 if so   Your bmi is 000023.889 if so   Your bmi is     23.889 if so   Your bmi is 23.889     if so   Your bmi is   23.889   if so   Your bmi is     23.889 if so   Your bmi is 23.889462809917354 if so   for height 1.76 and weight 74.0 the bmi is     23.889   for height 1.76 and weight 74.0 the bmi is     23.889   for height 1.76 and weight 74.0 the bmi is     23.889   munchkin:cambx grahamellis$ This ...
4593Command line parameter handling in Python via the argparse module
A new example from the Python course just completed - looking at command line parameters through the argparse module from the standard Python library (2.7 and 3.2 onwards), and XML handling through the xml.etree.ElementTree module (2.5 onwards) Here's code setting up an argument parser:   parser ...
4360Python - comparison of old and new string formatters
The % operator in Python when applied to a string object formats the object to the right using the format specifier to the left, using the same notations as the C function sprintf. For example:   >>> j = 1.0 / 7.0   >>> print j   0.142857142857   >>> print "j ...
4307Identifying and clearing denial of service attacks on your Apache server
If ... ..... .... I ..... ..... were ...... ... ... ... to . ...... . write .... .... .... . a ... ...... ... ..... sentence, .. ... but ...... .. drip ...... . ..... ...... ... the ..... ... .. ...... ...... words . . .. out ..... .. ...... ..... . slowly ..... ... ...... with .... ... ..... ... long ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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: >>> ...
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. ...
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 ...
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 ...
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. ...
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 ...
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 ...
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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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" ...
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 ...
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 ...
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
earlier   formatted printing variety - format method
fares.py   Distortion of bus prices due to concessionary fares
fence.py   Fencepost problem and solution
fhand   Unicode text string
formatting   formatting - old and new ways
funsub   Substitute for a regular expression
gift   Share 10000 pound between 1 to 10 children
grab   Regular Expression - Maya strings
ifi   generating text with character
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 and format method
p3.py   raw and regular strings
poundland   formatting a pound sign and a #! line
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
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 [here].
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 Ruby, Lua, 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.

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/resources/Y108.html • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb