Inspecting Strings


In [ ]:
# This is some IPython %magic
%xmode plain

In [ ]:
len("hello")

In [ ]:
"hello" + "!"

In [ ]:
"l" in "hello"

In [ ]:
"hello"[-3:]

Formatting Strings


In [ ]:
# Busted
count = 3
"Number of items: " + count

In [ ]:
# OK, but there's a better way
name = "Peter"
greeting = "Hello"
"%s, %s, what's happenning?" % (greeting, name)

In [ ]:
"{}, {}, what's happenning?".format(greeting, name)

In [ ]:
print "Number of items: ---{:05}---".format(count)
print "Number of items: ---{:>5}---".format(count)
print "Number of items: ---{:<5}---".format(count)

Joining and Splitting


In [ ]:
from string import ascii_lowercase
print ascii_lowercase
a_f = list(ascii_lowercase[:6])
print a_f
letters = ", ".join(letters)
print letters

In [ ]:
letters.split(", ")