>>> #operation in seuence >>> s = 'spam and ham' >>> s[0] 's' >>> s[1] 'p' >>> s[-1] 'm' >>> s[1:5] 'pam ' >>> s + s 'spam and hamspam and ham' >>> s * 5 'spam and hamspam and hamspam and hamspam and hamspam and ham' >>> 'a' in s True >>> 'z' in s False >>> len(s) 12 >>> >>> >>> str1 = 'Python is simple!' >>> str2 = "That's right." >>> str3 = "Don\'t just play." >>> long_str = 'long line\ continued..' >>> >>> multiple_lines = ''' first line second line third line ''' >>> print multiple_lines first line second line third line >>> >>> #Escape character (or Escape sequence) >>> print 'a\nb\nc' a b c >>> print 'a\012b\012c' a b c >>> >>> #Modification of string >>> s = 'spam and egg' >>> s = s[:5] + 'cheese ' + s[5:] >>> s 'spam cheese and egg' >>> >>> >>> #Formatting >>> '%s -- %s' % ([1,2,3],[4,5,6]) '[1, 2, 3] -- [4, 5, 6]' >>> >>> '%3d' % 1 ' 1' >>> '%3d' % 123456 '123456' >>> '%5.2f' % 1.23456 ' 1.23' >>> '%5.2f' % 1.23567 ' 1.24' >>> '%5.2f' % 9999991.23567 '9999991.24' >>> '%5.2d' % 9999991.23567 '9999991' >>> >>> print '%(name)s -- %(phone)s' % {'name':'Hyuk Cho', 'phone':4567} Hyuk Cho -- 4567 >>> D = {'name':'Hyuk Cho', 'phone':1234} >>> print '%(name)s abcde %(name)s %(phone)s' % D Hyuk Cho abcde Hyuk Cho 1234 >>> >>> >>> #Methods in string >>> s = 'spam and ham' >>> s.upper() 'SPAM AND HAM' >>> s.lower() 'spam and ham' >>> s.capitalize() 'Spam and ham' >>> s 'spam and ham' >>> s.count('a') 3 >>> >>> s.count('z') 0 >>> s.find('and') 5 >>> s.find('m') 3 >>> s.rfind('m') 11 >>> s.find('m', 4) 11 >>> s.find('egg') -1 >>> s.index('egg') Traceback (most recent call last): File "", line 1, in -toplevel- s.index('egg') ValueError: substring not found >>> s.index('and') 5 >>> try: k = s.index('egg') except ValueError: print 'egg not found' egg not found >>>