Python – Strings and Console Output

Strings and Console Output

I Strings

i. Strings 1/16

brian = “Hello life!”

ii. Practice 2/16

# 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

iii. Escaping Characters 3/16

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

‘This isn\’t flying, this is falling with style!’

iv. Access by Index

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

II. String Methods

i. String Methods 5/16

parrot = “Norwegian Blue”
print len(parrot)

ii. lower() 6/16

parrot = “Norwegian Blue”
print parrot.lower()

iii. upper() 7/16

parrot = “norwegian blue”

print parrot.upper()

iv. str() 8/16

“””Declare and assign your variable on line 4,
then call your method on line 5!”””

pi = 3.14
print str(pi)

v. Dot Notation 9/16

ministry = “The Ministry of Silly Walks”

print len(ministry)
print ministry.upper()

III Print

i. Printing Strings 10/16

“””Tell Python to print “Monty Python”
to the console on line 4!”””
print “Monty Python”

ii. Printing Variables  11/16

“””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

IV. Advanced Printing

i. String Concatenation 12/16

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

ii. Explicit String Conversion 13/16

# Turn 3.14 into a string on line 3!

print “The value of pi is around ” + str(3.14)

iii. String Formatting with %, Part 1, 14/16

string_1 = “Camelot”
string_2 = “place”

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

iv. String Formatting with %, Part 1, 15/16

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)

v. And Now, For Something Completely Familiar 16/16

# Write your code below, starting on line 3!

my_string = “I love chocolates”
print len(my_string)
print my_string.upper()


One thought on “Python – Strings and Console Output

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s