Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

String Methods

def stringMethods():

  #The .capitalize() method
  #Capitalizes the first letter in the string
  print '.capitalize() Method'
  ydo = 'yaba daba do'
  print ydo
  print ydo.capitalize()
  #A few wrong examples
  #print ydo.capitalize
  #print capitalize(ydo)
  #print 12.capitalize()
  print('\n')

  #The .startswith(prefix) method
  # returns 0 if false, 1 if true
  print '.startswith() Method'
  fstone = 'Mr. Fred Flintstone'
  print fstone.startswith('Mr.')
  print fstone.startswith('Ms.')
  print('\n')

  #The .endswith(suffix) method
  # returns 0 if false, 1 if true
  print '.endswith() Method'
  file = 'my_picture.gif'
  print file.endswith('jpg')
  print file.endswith('gif')
  print('\n')

  #The .find(findstring), .find(findstring,start), .find(findstring,start,end), rfind(findstring) methods
  #returns -1 if not found
  #index of found string if found
  print '.find() Methods'
  tsong = "I'm a ramblin wreck from Georgia Tech and a Helluva Engineer"
  print tsong.find('UGA')
  print tsong.find('Tech')
  print tsong.find('Tech', 34)
  print tsong.find('Tech', 3, 25)
  print tsong.rfind('Tech')
  print('\n')

  #The .upper(), .lower(), .swapcase(), .title() methods
  #Make the string all upper, lower, opposite or proper case
  print '.upper(), .lower(), .swapcase(), .title() Methods'
  hj = 'Homer j. Simpson'
  print hj.upper()
  print hj.lower()
  print hj.swapcase()
  print hj.title()
  print('\n')


  #The .isalpha(), .isdigit() methods
  #check for all letters, all digits
  print '.isalpha() Method'
  empty = ''
  achk0= 'Iam6monthsold'
  achk1= 'I am six months old'
  achk2= 'Iamsixmonthsold'
  print empty.isalpha()
  print achk0.isalpha()
  print achk1.isalpha()
  print achk2.isalpha()
  print '.isdigit() Method'
  dchk0='1234567E'
  dchk1='1 2 3 4 5 6 7'
  dchk2='1234567'
  print empty.isdigit()
  print dchk0.isdigit()
  print dchk1.isdigit()
  print dchk2.isdigit()
  print('\n')

  #The .replace(search, replace) method
  #searches for search and replaces with replace
  #returns result but does NOT change original string
  #if not found, returns original string
  print '.replace(search, replace) Method'
  lis = 'Life is good'
  print lis.replace('good', 'pizza')
  print lis
  print lis.replace('Life', 'Pizza')
  print lis.replace('bad', 'better')
  print('\n')
  
  #The .split(delimiter) method
  #splits a string at its delimiter
  print '.split() Method'
  print lis.split(' ')
  print('\n')


Link to this Page