# define function to pause in tutorial def pause(): raw_input('...') print '\n'*24 # strings: example of a long string spanning several lines print """>>> stringOne = 'this is a long\ """ print """string of several lines\ """ print """by using backslash (often works in Unix)'""" print '>>> print StringOne' stringOne = 'this is a long\ string of several lines\ by using backslash (often works in Unix)' print stringOne pause() # strings: example of long string of several lines print """>>> stringTwo = '''this is an example of a several line-long string that includes newline characters'''""" print '>>> print stringTwo' stringTwo = '''this is an example of a several line-long string that includes newline characters''' print stringTwo pause() # strings: examples of built-in methods print ">>> len(stringOne), len(stringTwo)\n", len(stringOne), len(stringTwo) pause() # hunting for the character 'n' print ">>> stringOne.index('n')\n", stringOne.index('n') pause() # testing for a character or string print ">>> 'a' in stringOne\n", 'a' in stringOne pause() print ">>> 'this' in stringOne\n", 'this' in stringOne pause() # show that indexing works for string print ">>> stringOne[8]\n", stringOne[8] pause() # show that slicing works for string print ">>> stringOne[7:25]\n", stringOne[7:25] pause() # convert number into a string print ">>> type(7) = ", type(7) print ">>> type('abc') =", type('abc') print ">>> type(str(7)) = ", type(str(7)) pause() # convert words in a string into a sequence print ">>> s = stringTwo.split(' ')" s = stringTwo.split(' ') print s pause() # convert sequence of strings into a string print ">>> t = '_'.join(s)" t = '_'.join(s) print t pause() # formatting with a string print "using % to format with a string" print ">>> y = 'x is %d' % 7" y = 'x is %d' % 7 print 'y =',y pause() # formatting example print '>>> print "the field %s is %e" % ("hival",1593.997)' print "the field %s is %e" % ('hival',1593.997) pause()