Chapter 8 Strings
s = "abcde" print(len(s)) # note does not use dot notation print(s[len(s)]) # error! print(s[-1]) # gives the last character print(s[len(s)-1]) # also gives the last character print(s[len(s)]-1) # error - [] is higher priority
st = "Kentucky" for i in st: print(i) for i in range(len(st)): print(st[i])
i = 0 found = False st = "WORD" while i < len(st) and not found: if st[i] == "A" or st[i] == "E" or st[i] == "I" or st[i] == "O" or st[i] == "U": found = True i +=1
if not (ch in st):but you can also use
if ch not in st:
print("Enter 5 words. Press enter between each one.") sentence = "" for i in range(5): wd = input("Enter a word") sentence = sentence + wd print("the complete sentence is", sentence)
def pow (base): return base * base
def pow(base, ex): res = 1 for i in range(ex): res = res * base return res
def fun (parm1, parm2, parm3=value, parm4=value, ...):
def pow(base, ex=2): res = 1 for i in range(ex): res = res * base return res
from string import * def main(): s = "abc123%^&" print(s in ascii_lowercase) print(s[2] in ascii_lowercase) print(s in digits) print(s[3] in digits) print(s in punctuation) print(s[6] in punctuation) main()