Please send questions to st10@humboldt.edu .
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) 
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "copyright", "credits" or "license()" for more information.

IDLE 1.2      

# adding some additional comments after-class, in hopes of making
#    these examples more useful...

# concatenation-related examples

>>> print 'the number of orders is' + str(3)
the number of orders is3

# in Python, concatenation must involve strings

>>> val1 = 3
>>> val2 = 7
>>> print 'the values are ' + val1 + str(val2)

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    print 'the values are ' + val1 + str(val2)
TypeError: cannot concatenate 'str' and 'int' objects

# "traditional" C/C++/Java for-loop not supported in Python

>>> for i=3; i<5; i++:
	
SyntaxError: invalid syntax

# string-related examples: single or double quotes are fine...

>>> type("Hello")
<type 'str'>

>>> "Hello"
'Hello'

>>> 'Hello'
'Hello'

>>> "Hello" == 'Hello'
True

# ...and having both as options is sometimes convenient

>>> print "george's"
george's

>>> print 'She said, "Hello"'
She said, "Hello"

>>> "george's" + '"Hi lo in between"'
'george\'s"Hi lo in between"'

>>> print "george's" + '"Hi lo in between"'
george's"Hi lo in between"

# string literals don't have to have a + to be concatenated...!

>>> print "hi" 'lo' "there"
hilothere

# ...but it appears that omitting the + only works for string *literals*

>>> print "value is" 3
SyntaxError: invalid syntax

>>> print "value is" str(3)
SyntaxError: invalid syntax

>>> print "value is " + str(3)
value is 3

# str can be used to get a string depiction of just about any Python object

>>> def babyfunct(val1, val2):
	print str(val1) + str(val2)
	
>>> str(babyfunct)
'<function babyfunct at 0x1108b30>'

>>> babyfunct(3, 4)
34

# note that babyfunct doesn't *return* anything... it prints and then ends.
#    Below, then, you see babyfunct's action, and then what it returned ---
#    nothing, None, the special NoneType value --- in its str() depiction.

>>> str( babyfunct(3, 4) )
34
'None'

# if you put commas between print values --- you get a space between those
#    values in the output!

>>> print str(1), str(2), str(3)
1 2 3

>>> print str(1) str(2) str(3)
SyntaxError: invalid syntax

>>> print str(1),str(2),str(3)
1 2 3

>>> count = 5
>>> i = 0
>>> while i < count:
	i = i + 1
	print i

	
1
2
3
4
5

>>> i = 0
>>> while i < count:
	i = i + 1
	print i,

	
1 2 3 4 5

# playing with escape sequences -- \ in front of a character indicates
#    something "special"

# \n is a newline, \t is a tab

>>> sentence = 'a\nb\tc'
>>> sentence
'a\nb\tc'

>>> print sentence
a
b	c

>>> print 'a\tab\tabc\tabcd'
a	ab	abc	abcd

# \ followed by a newline continues the line --- the newline is ignored

>>> print 'The rain in Spain stays mainly in the plain \
In Spain! In Spain!'
The rain in Spain stays mainly in the plain In Spain! In Spain!

# \\ is a backslash character

>>> mypath = 'c:\\here\\it\\is'
>>> mypath
'c:\\here\\it\\is'

>>> print mypath
c:\here\it\is

# raw strings: if a string literal is preceded by r or R, backslashes
#    have no special meaning

>>> mypath2 = r'c:\here\it\is'
>>> print mypath2
c:\here\it\is

>>> mypath == mypath2
True

# triple-quoted strings are convenient for multi-line blocks of text

>>> lyric = "Spam spam spam spam\nSpam spam spam spam"
>>> lyric
'Spam spam spam spam\nSpam spam spam spam'

>>> print lyric
Spam spam spam spam
Spam spam spam spam

>>> lyric2 = '''Spam spam spam spam
Spam spam spam spam'''

>>> print lyric2
Spam spam spam spam
Spam spam spam spam

>>> lyric == lyric2
True

>>> lyric3 = """Spam spam spam spam
Spam spam spam spam


Spam spam spam SPAM!!!!!"""

>>> print lyric3
Spam spam spam spam
Spam spam spam spam


Spam spam spam SPAM!!!!!

>>> lyric3
'Spam spam spam spam\nSpam spam spam spam\n\n\nSpam spam spam SPAM!!!!!'

# u in front of a string literal makes that string a Unicode string

>>> u'spam'
u'spam'

>>> type(u'spam')
<type 'unicode'>

>>> type('spam')
<type 'str'>

# "regular" strings and Unicode strings can play easily and well together,
#    in Python...!

>>> food1 = u'spam'
>>> food2 = 'spam'
>>> food1 == food2
True

>>> type(food1)
<type 'unicode'>

>>> type(food2)
<type 'str'>

>>> food1 is food2
False

# when you concatenate Unicode and "regular" strings, the result is
#    in Unicode

>>> food1 + food2
u'spamspam'

>>> type(food1 + food2)
<type 'unicode'>

# unicode(str) returns a Unicode version of the string str

>>> unicode(food2)
u'spam'

# len(str) returns the length of the string str

>>> len(food1)
4

>>> len(food2)
4

>>> len('a\nb\tc')
5

# how you can get a new string that is another string repeated 

>>> 'Ni!'*4
'Ni!Ni!Ni!Ni!'

>>> "*"*80
'********************************************************************************'

>>> food1
u'spam'

# array-style indexing of strings - remember that 0 is the index of the
#    first element in a string

>>> phrase = "Howdy"
>>> len(phrase)
5

>>> phrase[0]
'H'

>>> i = 0
>>> while i < len(phrase):
	print phrase[i]
	i = i + 1

	
H
o
w
d
y

# like Perl, the index -1 retrieves the last element, -2 retrieves the 
#    second-to-last element, etc.

>>> phrase[-1]
'y'

>>> phrase[-2]
'd'

# the in operator is true if the thing on the left is contained within
#    the thing on the right

>>> "o" in "Howdy"
True

>>> "ow" in "Howdy"
True

>>> letter = "o"
>>> letter in phrase
True

>>> url = "http://www.humboldt.edu"
>>> ":" in url
True

>>> rel_url = "here/there"
>>> ":" in rel_url
False

>>> ":" not in rel_url
True

>>> answer = ":" not in rel_url

>>> answer
True

>>> if ":" not in rel_url:
	print "http://" + rel_url

	
http://here/there

>>> punct = '.!?,'

>>> "Howdy!" in punct
False

>>> phrase[-1] in punct
False

>>> phrase
'Howdy'

>>> phrase = phrase + "!"

>>> phrase
'Howdy!'

>>> phrase[-1] in punct
True

>>> def ends_in_punct(phrase):
	punct = '!?.,'
	if phrase[-1] in punct:
		return True
	else:
		return False

	
>>> ends_in_punct("Hello")
False
>>> ends_in_punct("Howdy, partner!")
True

>>> greeting = "Hello."

>>> # can grab "chunks" of strings using slices
>>> # stringy[a:b]  I grab the chunk/slice from index a
>>> #    up to but not including index b

>>> greeting
'Hello.'

>>> greeting[1:3]
'el'

>>> phrase = "abcdefghijk'
SyntaxError: EOL while scanning single-quoted string

>>> phrase = "abcdefghijk"
>>> phrase[2:6]
'cdef'

>>> phrase[2:-1]
'cdefghij'

# no second number in a slice? goes to end of string...

>>> phrase[2:]
'cdefghijk'

# no first number in a slice? starts at beginning of string...

>>> phrase[:6]
'abcdef'

# neither number in a slice? supposedly creates a to-level copy
#    of that string...!

>>> phrase[:]
'abcdefghijk'

>>> phrase1 = phrase
>>> phrase1 is phrase
True

>>> phrase2 = phrase[:]
>>> phrase2 is phrase
True

>>> phrase = "A longer phrase thank you very much" * 8
>>> phrase2 = phrase[:]

>>> phrase2 == phrase
True

>>> phrase2 is phrase
True

>>> phrase3 = phrase[:]
>>> phrase2 is phrase3
True

# reminder: for..in loop and strings: can "walk" through the characters
#    in a string using such a loop

>>> for letter in greeting:
	print letter,

	
H e l l o .

# some string methods

>>> "H".lower()
'h'

>>> "h".upper()
'H'

>>> "H".isupper()
True

>>> "H".islower()
False

>>> greeting
'Hello.'
>>> for letter in greeting:
	if letter.isupper():
		print letter.lower()
	else:
		print letter.upper()

		
h
E
L
L
O
.

# some string formatting examples

>>> 1 / 3.0
0.33333333333333331

>>> print "The result is: " + str(1 / 3.0)
The result is: 0.333333333333

>>> print "The result is: %6.3f" % (1 / 3.0)
The result is:  0.333

>>> i = 0
>>> count
5
>>> while i < count:
	print "%5.2f" % (1/ (i+1))
	i = i + 1

	
 1.00
 0.00
 0.00
 0.00
 0.00

>>> i = 0
>>> while i < count:
	print "%5.2f" % (1.0/ (i+1))
	i = i + 1

	
 1.00
 0.50
 0.33
 0.25
 0.20

>>> while i < count:
	print "%-8s" % (str(i) * i)
	i = i + 1

	
        
1       
22      
333     
4444    

>>> i = 0
>>> while i < count:
	print "%8s" % (str(i) * i)
	i = i + 1

	
        
       1
      22
     333
    4444

>>> i = 0
>>> while i < 15:
	print "%4d" % i
	i = i + 1

	
   0
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14

# more examples of string methods

>>> "howdy".capitalize()
'Howdy'

>>> "howdy".center(10)
'  howdy   '

>>> "howdy".find('o')
1

>>> "lululululululululu".find('lu', 5, 10)
6

>>> "2".isdigit()
True

>>> "23".isdigit()
True

>>> "23a".isdigit()
False

>>> "abc".isalpha()
True

>>> "abc12".isalpha()
False

>>> " ".isspace()
True

>>> "\t".isspace()
True

>>> phrase = "This is it."
>>> for letter in phrase:
	if letter.isspace():
		print "!",
	else:
		print letter

		
T
h
i
s
! i
s
! i
t
.

>>> for letter in phrase:
	if letter.isspace():
		print "!",
	else:
		print letter,

		
T h i s ! i s ! i t .
>>>