Main Content
Strings as collections in Python Archive - Originally posted on "The Horse's Mouth" - 2009-07-12 17:30:54 - Graham Ellis
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 have",breakfast
Dorothy-2:py grahamellis$ python sar2
Give me a C
Give me a r
Give me a o
Give me a i
Give me a s
Give me a s
Give me a a
Give me a n
Give me a t
Give me a s
Give me a
Give me a a
Give me a n
Give me a d
Give me a
Give me a t
Give me a o
Give me a a
Give me a s
Give me a t
and we have Croissants and toast
Dorothy-2:py grahamellis$
Some other articles
Y108 - String Handling Prining a pound sign from Python AND running from the command line at the same time Python formatting update - including named completions Command line parameter handling in Python via the argparse module Python - comparison of old and new string formatters Identifying and clearing denial of service attacks on your Apache server Formatting options in Python Why are bus fares so high? Collections in Python - list tuple dict and string. Formatting output - why we need to, and first Python example Backquote, backtic, str and repr in Python - conversion object to string Teaching dilemma - old tricks and techniques, or recent enhancements? Python string formatting - the move from % to str.format Formatting output in Python through str.format Matching a license plate or product code - Regular Expressions Matching to a string - what if it matches in many possible ways? Python - splitting and joining strings Formatted Printing in Python Running operating system commands from your Python program Regular Expressions in Python Flexible search and replace in Python Pound Sign in Python Program This article Python Regular Expressions Underlining in Perl and Python - the x and * operator in use Python - formatting objects Regular Express Primer Python - two different splits String duplication - x in Perl, * in Python and Ruby Splitting Pythons in Bradford Matching within multiline strings, and ignoring case in regular expressions Pieces of Python Breaking bread The fencepost problem Python printf Splitting the difference The backtick operator in Python and Perl Y104 - Lists and Tuples Embedding more complex code into a named block Shuffling a list - Ruby and Python Collections in Python - list tuple dict and string. Spike solutions and refactoring - a Python example Stepping through a list (or an array) in reverse order List slices in Python - 2 and 3 values forms, with an uplifting example All possible combinations from a list (Python) or array (Ruby) Beware - a=a+b and a+=b are different - Python Arrays of arrays - or 2D arrays. How to program tables. Copying - duplicating data, or just adding a name? Perl and Python compared Traffic lights in Python Python - fresh examples of all the fundamentals This article Creating and iterating through Python lists Looking for a value in a list - Python Tektronix 4010 series / Python Tuples for loop - how it works (Perl, PHP, Java, C, etc) Python collections - mutable and imutable Python - extend v append on a list The ternary operator in Python Overloading of operators on standard objects in Python