activity 1

  • strings
    • a data type that can include letters, numbers, and symbols
  • there are some characters that cause problems
    • 'there's a snake in my boot!'
      • python thinks that the apostrophe in there's ends the string
    • fix: 'there\'s a snake in my boot!'
      • (be sure to use forward slash, not back slash
  • each character in a string is assigned a number, which is called the index
    • c = "cats" [3]
      • in python, you start counting from 0; so, 0 = c, 1 = a, 2 = t, 3 = s

strings methods

  • string methods let you perform specific tasks for strings
    • four string methods
      • len ()
      • lower ()
      • upper ()
      • str ()
  • line 1 parrot = "Norwegian Blue"
  • line 2 print len(parrot)
    • the output will be the length of "Norwegian Blue"
  • lower() gets rid of all the capitalization in your strings
    • print parrot.lower()
  • upper() can make a string upper case
    • "the Ministry of Silly Walks".upper()
      • whatever I wants capitalized needs to go in parenthesis
  • str()turns non strings into strings

dot notation

  • methods that use dot notation only work with strings
  • however, len() and str() can work with other data types

printing strings

  • editor: where we write the code
  • console: where results of the code are shown
  • print: displays code in console
    • print "Monty Python"
    • print Monty Python
    • why do I need to add quotation marks sometimes and not others?
  • concatenation: combining strings
    • print "Life " + "of " + "Brian"
      • no space after quotation mark on last word
  • explicit string conversion
    • sometimes you need to combine a string with something that isn't a string
    • to do that, convert the nonstring into a string str()
  • % after a string combines strings with variables
    • print "Hello %s" % (name)
      • %s will be replaced by (name)
nov 28 2014 ∞
apr 19 2015 +