10/14/13

Strings recap


In [1]:
s = 'copycat'
print len(s)


7

In [2]:
print s[:7]


copycat

In [3]:
print s[:6]


copyca

In [5]:
s = 't'

In [6]:
s = 'copycat'
for letter in s:
    print letter


c
o
p
y
c
a
t

In [7]:
dir(s)


Out[7]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__getslice__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__len__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmod__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '_formatter_field_name_split',
 '_formatter_parser',
 'capitalize',
 'center',
 'count',
 'decode',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'index',
 'isalnum',
 'isalpha',
 'isdigit',
 'islower',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']

In [9]:
i = s.find('y')
print s[i]


y

In [10]:
if 'y' in s:
    print "There's a Y!!"
else:
    print "NO Y!!"


There's a Y!!

In [15]:
if 'y' in raw_input('Do you want to quit (Y/N)?').lower():
    print "Quitting"
else:
    print "Keep going"


Do you want to quit (Y/N)?Y
Quitting

In [21]:
print "words".upper().capitalize().lower()[:2]


wo

In [25]:
print 'apple' == 'banana'
print 'apple' < 'banana'
print 'apple'.lower() < 'Banana'.lower()


False
True
True

In [ ]: