Strings & Console Output

Lesson 01/16 – Strings

[code lang=”python”]

Set the variable brian on line 3!

brian = “Hello life!”
[/code]

Lesson 02/16 – Practice

[code lang=”python”]

Assign your variables below, each on its own line!

caesar = “Graham”
praline = “John”
viking = “Teresa”

Put your variables above this line

print caesar
print praline
print viking
[/code]

Lesson 03/16 – Escaping characters

[code lang=”python”]

The string below is broken. Fix it using the escape backslash!

‘This isn\’t flying, this is falling with style!’
[/code]

Lesson 04/16 – Access by Index

[code lang=”python”]
“””
The string “PYTHON” has six characters,
numbered 0 to 5, as shown below:

+—+—+—+—+—+—+
| P | Y | T | H | O | N |
+—+—+—+—+—+—+
  0   1   2   3   4   5

So if you wanted “Y”, you could just type
“PYTHON”[1] (always start counting from 0!)
“””
fifth_letter = “MONTY”[4]

print fifth_letter
[/code]

Lesson 05/16 – String methods

[code lang=”python”]
parrot = “Norwegian Blue”
print len(parrot)
[/code]

Lesson 06/16 – lower()

[code lang=”python”]
parrot = “Norwegian Blue”

print parrot.lower()
[/code]

Lesson 07/16 – upper()

[code lang=”python”]
parrot = “norwegian blue”

print parrot.upper()
[/code]

Lesson 08/16 – str()

[code lang=”python”]
“””Declare and assign your variable on line 4,
then call your method on line 5!”””

pi = 3.14
print str(pi)
[/code]

Lesson 09/16 – Dot Notation

[code lang=”python”]
ministry = “The Ministry of Silly Walks”

print len(ministry)
print ministry.upper()
[/code]

Lesson 10/16 – Printing Strings

[code lang=”python”]
“””Tell Python to print “Monty Python”
to the console on line 4!”””

print “Monty Python”
[/code]

Lesson 11/16 – Printing Variables

[code lang=”python”]
“””Assign the string “Ping!” to
the variable the_machine_goes on
line 5, then print it out on line 6!”””

the_machine_goes = “Ping!”
print the_machine_goes
[/code]

Lesson 12/16 – String Concatenation

[code lang=”python”]

Print the concatenation of “Spam and eggs” on line 3!

print “Spam ” + “and” + ‘ eggs’
[/code]

Lesson 13/16 – Explicit String Conversion

[code lang=”python”]

Turn 3.14 into a string on line 3!

print “The value of pi is around ” + str (3.14)
[/code]

Lesson 14/16 – String Formatting with %, Part 1

[code lang=”python”]
string_1 = “Camelot”
string_2 = “place”

print “Let’s not go to %s. ‘Tis a silly %s.” % (string_1, string_2)
[/code]

Lesson 15/16 – String Formatting with %, Part 2

[code lang=”python”]
name = raw_input(“What is your name?”)
quest = raw_input(“What is your quest?”)
color = raw_input(“What is your favorite color?”)

print “Ah, so your name is %s, your quest is %s, ” \
“and your favorite color is %s.” % (name, quest, color)
[/code]

Lesson 16/16 – And Now, For Something Completely Familiar

[code lang=”python”]

Write your code below, starting on line 3!

my_string = “stringa”
print len(my_string)
print my_string.upper ()
[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax