>>> 'shrubbery', "shrubbery"
('shrubbery', 'shrubbery')



>>> 'knight"s', "knight's"
('knight"s', "knight's")



>>> title = "Meaning " 'of' " Life"         # implicit concatenation
>>> title
'Meaning of Life'



>>> 'knight\'s', "knight\"s"
("knight's", 'knight"s')



>>> s = 'a\nb\tc'
>>> s
'a\nb\tc'
>>> print s
a
b       c
>>> len(s)
5



>>> s = 'a\0b\0c'
>>> s
'a\x00b\x00c'
>>> len(s)
5



>>> s = '\001\002\x03'
>>> s
'\x01\x02\x03'
>>> len(s)
3



>>> x = "C:\py\code"     # keeps \ literally
>>> x
'C:\\py\\code'
>>> len(x)
10



myfile = open(r'C:\new\text.dat', 'w')
myfile = open('C:\\new\\text.dat', 'w')



>>> path = r'C:\new\text.dat'
>>> path                          # Show as Python code.
'C:\\new\\text.dat'
>>> print path                    # User-friendly format
C:\new\text.dat
>>> len(path)                     # String length
15



>>> mantra = """Always look
...  on the bright
... side of life."""
>>>
>>> mantra
'Always look\n on the bright\nside of life.'



X = 1
"""
import os
print os.getcwd()
"""
Y = 2



>>> u'spam'
u'spam'



>>> 'ni' + u'spam'        # Mixed string types
u'nispam'



>>> str(u'spam')          # Unicode to normal
'spam'
>>> unicode('spam')       # Normal to unicode
u'spam'



>>> u'ab\x20cd'           # 8-bit/1-byte characters
u'ab cd'
>>> u'ab\u0020cd'         # 2-byte characters
u'ab cd'
>>> u'ab\U00000020cd'     # 4-byte characters
u'ab cd'



% python
>>> len('abc')         # Length: number items 
3
>>> 'abc' + 'def'      # Concatenation: a new string
'abcdef'
>>> 'Ni!' * 4          # Repetition: like "Ni!" + "Ni!" + ...
'Ni!Ni!Ni!Ni!'



>>> print '------- ...more... ---'      # 80 dashes, the hard way
>>> print '-'*80                        # 80 dashes, the easy way



>>> myjob = "hacker"
>>> for c in myjob: print c,       # step through items
...
h a c k e r
>>> "k" in myjob                   # found
True
>>> "z" in myjob                   # not found
False



>>> S = 'spam'
>>> S[0], S[-2]               # Indexing from front or end
('s', 'a')
>>> S[1:3], S[1:], S[:-1]     # Slicing: extract section
('pa', 'pam', 'spa')



>>> S = 'abcdefghijklmnop'
>>> S[1:10:2]
'bdfhj'
>>> S[::2]
'acegikmo'



>>> S = 'hello'
>>> S[::-1]
'olleh'



>>> S = 'abcedfg'
>>> S[5:1:-1]
'fdec'



# File echo.py
import sys
print sys.argv

% python echo.py -a -b -c
['echo.py', '-a', '-b', '-c']



>>> "42" + 1
TypeError: cannot concatenate 'str' and 'int' objects
>>> int("42"), str(42)         # Convert from/to string.
(42, '42')
>>> repr(42), `42`             # Convert to as-code string.
('42', '42')



>>> S = "42"
>>> I = 1
>>> S + I                
TypeError: cannot concatenate 'str' and 'int' objects

>>> int(S) + I               # Force addition.
43

>>> S + str(I)               # Force concatenation.
'421'



>>> str(3.1415), float("1.5")
('3.1415', 1.5)

>>> text = "1.234E-10"
>>> float(text)
1.2340000000000001e-010



>>> ord('s')
115
>>> chr(115)
's'



>>> S = '5'
>>> S = chr(ord(S) + 1)
>>> S
'6'
>>> S = chr(ord(S) + 1)
>>> S
'7'



>>> int('5')
5
>>> ord('5') - ord('0')
5



>>> B = '1101'
>>> I = 0
>>> while B:
        I = I * 2 + (ord(B[0]) - ord('0'))
        B = B[1:]
	
>>> I
13



>>> S = 'spam'
>>> S[0] = "x"
Raises an error!



>>> S = S + 'SPAM!'       # To change a string, make a new one.
>>> S
'spamSPAM!'
>>> S = S[:4] + 'Burger' + S[-1]
>>> S
'spamBurger!'



>>> S = 'splot'
>>> S = S.replace('pl', 'pamal')
>>> S
'spamalot'



>>> 'That is %d %s bird!' % (1, 'dead')    # like C sprintf
That is 1 dead bird!



>>> exclamation = "Ni"
>>> "The knights who say %s!" % exclamation
'The knights who say Ni!'

>>> "%d %s %d you" % (1, 'spam', 4)
'1 spam 4 you'

>>> "%s -- %s -- %s" % (42, 3.14159, [1, 2, 3])
'42 -- 3.14159 -- [1, 2, 3]'




>>> x = 1234
>>> res = "integers: ...%d...%-6d...%06d" % (x, x, x)
>>> res
'integers: ...1234...1234  ...001234'



>>> x = 1.23456789
>>> x
1.2345678899999999

>>> '%e | %f | %g' % (x, x, x)
'1.234568e+000 | 1.234568 | 1.23457'



>>> '%-6.2f | %05.2f | %+06.1f' % (x, x, x)
'1.23   | 01.23 | +001.2'

>>> "%s" % x, str(x)
('1.23456789', '1.23456789')



>>> "%(n)d %(x)s" % {"n":1, "x":"spam"}
'1 spam'



>>> reply = """
Greetings...
Hello %(name)s!
Your age squared is %(age)s
"""
>>> values = {'name': 'Bob', 'age': 40}
>>> print reply % values

Greetings...
Hello Bob!
Your age squared is 40



>>> food = 'spam'
>>> age = 40
>>> vars()
{'food': 'spam', 'age': 40, ...many more... }
>>> "%(age)d %(food)s" % vars()
'40 spam'



>>> S = 'spammy'
>>> S = S[:3] + 'xx' + S[5:]
>>> S
'spaxxy'



>>> S = 'spammy'
>>> S = S.replace('mm', 'xx')
>>> S
'spaxxy'



>>> 'aa$bb$cc$dd'.replace('$', 'SPAM')
'aaSPAMbbSPAMccSPAMdd'



>>> S = 'xxxxSPAMxxxxSPAMxxxx'
>>> where = S.find('SPAM')          # Search for position
>>> where                           # Occurs at offset 4
4
>>> S = S[:where] + 'EGGS' + S[(where+4):]
>>> S
'xxxxEGGSxxxxSPAMxxxx'



>>> S = 'xxxxSPAMxxxxSPAMxxxx'
>>> S.replace('SPAM', 'EGGS')           # Replace all
'xxxxEGGSxxxxEGGSxxxx'

>>> S.replace('SPAM', 'EGGS', 1)        # Replace one
'xxxxEGGSxxxxSPAMxxxx'



>>> S = 'spammy'
>>> L = list(S)
>>> L
['s', 'p', 'a', 'm', 'm', 'y']
>>> L[3] = 'x'              # Works for lists, not strings
>>> L[4] = 'x'
>>> L
['s', 'p', 'a', 'x', 'x', 'y']
>>> S = ''.join(L)
>>> S
'spaxxy'



>>> 'SPAM'.join(['eggs', 'sausage', 'ham', 'toast'])
'eggsSPAMsausageSPAMhamSPAMtoast'



>>> line = 'aaa bbb ccc'
>>> col1 = line[0:3]
>>> col3 = line[8:]
>>> col1
'aaa'
>>> col3
'ccc'



>>> line = 'aaa bbb   ccc'
>>> cols = line.split()
>>> cols
['aaa', 'bbb', 'ccc']



>>> line = 'bob,hacker,40'
>>> line.split(',')
['bob', 'hacker', '40']



>>> line = "i'mSPAMaSPAMlumberjack"
>>> line.split("SPAM")
["i'm", 'a', 'lumberjack']



>>> line = "The knights who sy Ni!\n"
>>> line.rstrip()
'The knights who sy Ni!'
>>> line.upper()
'THE KNIGHTS WHO SY NI!\n'
>>> line.isalpha()
False
>>> line.endswith('Ni!\n')
True



>>> line
'The knights who sy Ni!\n'

>>> line.find('Ni') != -1          # search via method call or expression
True
>>> 'Ni' in line
True

>>> sub = 'Ni!\n'
>>> line.endswith(sub)             # end test via method call or slice
True
>>> line[-len(sub):] == sub
True



>>> S = 'a+b+c+'
>>> x = S.replace('+', 'spam')
>>> x
'aspambspamcspam'



>>> import string
>>> y = string.replace(S, '+', 'spam')
>>> y
'aspambspamcspam'

