Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings: What can we do with Strings?

Similar presentations


Presentation on theme: "Strings: What can we do with Strings?"— Presentation transcript:

1

2 Strings: What can we do with Strings?
concatenate (join) + make multiple copies using * We can compare! > < == >= <= != With strings, cat < dog < zip Technically, based on the ASCII value (a numeric equivalent for each character) of the letters in a word. No one remembers the asci values of letters Instead, think of what page in the dictionary a word would occur on (about). So cat might occur on page 20, zip would occur much later in the dictionary, like maybe on page 580. 20< 580, so “cat” < “dog” What else can we do?

3 A lot! Len, in def f(x): if "e" in x: return("e is in your message.")
else: return("e is not in your message.") print(f(“puppies are cute”)) z = len("cat")

4 Strings Positional Get a character at a particular index within a string stringvar1 = “ binary” Index stringvar2 = “ computer” Index So to use a particular item in the list: x=stringvar2[3] #”p” y=stringvar1[1] #”i” z=stringvar2[0] #”c” w=stringvar1[6] #??? Example: def f(par): randnum = randrange(0,6) return(“you get “ + par[randnum]) print(f((stringvar1))

5 String: stringvar1 = “binary” Index 012345 stringvar2 = “ computer”
get the length of a string len(stringvar1) # will give you 6, not 5! len(stringvar2) #will give you 8, not 7 Example: def f(par): y = len(par) randnum = randrange(0,y) #Why does this work? return(“you get “ + par[randnum]) print(f(stringvar2))

6 We can now do: f(“kluge”) def f(y): x = 0 while (x < len(y)):
print(y[x]) x+=1 return f(“kluge”)

7 Slicing (Different from Indexing)
Copying parts of strings: | p | i | z | z | a | def f(word): return(word[2:5] ) print(f(“pizza”)) def g(): word = “pizza” return(word[1:3]) def h(): return(word[-4:-2]) def i(): return(word[-4:3])

8 Shortcuts | p | i | z | z | a | word=“pizza” word[0:4] pizz word[:4] word[2:5] zza word[2:]

9 # display a slice def g(s,f,wd): return("wd["+str(s)+":"+str(f)+"] is "+wd[s:f]) print(g(3,7,"sesquipedalian"))

10 Strings are Immutable word of the day
Can: x = “leprechaun" print(x.count(‘e’)) # counts the number of e’s print(x.index(‘e')) # prints the first index of e Can’t (if it changes the string, we can’t do it) x[3] = “y” #can’t do!

11 What does this do? def f(str1): str2 = “WuGmUmP” if str1.upper() == str2.upper(): return(“You’re one of us!”) else: return(“You’re not one of us!”) newstr = “wuGMuMp” print(f(newstr))

12 def func(a,y): x = 0 tot = 0 while (x < len(a)): if (a[x] in y): tot += 1 x += 1 return(tot)  a = “ambiguous" y = "aeiou" print(str(func(a,y)))

13 def f3(x): z = "" y = 0 while (y < len(x)): if x[y] == “m": z = z + “p" else: z = z+ x[y] y += 1 return z print(f3(“mummy"))

14 def f(a): k = True x= 0 while x< (len(a)+1)//2: if a[x]
def f(a): k = True x= 0 while x< (len(a)+1)//2: if a[x] != a[len(a)-(x+1)]: k = False x += 1 return k print(f(“racecar")) print(f("mommy"))

15 What about this? def g(y,z): x = len(y)-1 while x >= 0: z += y[x] #we can concatenate strings!!! x-=1 return z print(g("nilbog",""))

16 What does this do? def f(message): newmessage = "" x = 0 while x < len(message): if message[x] == "g": newmessage += "l" else: newmessage += message[x] x += 1 return(newmessage) print(f("pogysyggabicaggy“))

17 Largest of x random numbers
Write a function that takes 2 integers (x and y) as input parameters , and generates x random numbers between 0 and y. It finds the largest of those random numbers. def find_largest(x,y): largest = 0; count = 0; while count < x: rnum = randrange(0,y) if rnum > largest: largest = rnum count +=1 return(largest); print(find_largest(7,50))

18 Nested Loops: Remember?
def func1(x,y): k = 0 while (k < y): if (k ==x) or (k == (y-1)-x): print("X",end="") else: print("_",end="") k += 1 print(":") return def func2(y): ct = 0 while (ct < y): func1(ct,y) ct += 1 func2(5)

19 What do we get? Rules: The contents of the while loop is only what is indented under the while loop We do the contents of the while loop over and over until the while loop’s condition becomes false. Only then do we do what is below the while loop. Summary: we finish the all of the inner loop before going back to the outer loop def q(x): ct = 0 while (ct <= x): ct2 = 0 while (ct2 <= x): print("*",end = " ") ct2 += 1 print(" ") ct += 1 return q(4)

20 What do we get? def q(x,y): ct = 0 while (ct <= x): ct2 = 0 while (ct2 <= ct): print("*",end = " ") ct2 += 1 print(" ") ct += 1 return q(4,3)

21 What does this print out?
def h(x): ct = 1 while (ct <= x): ct2 = ct while (ct2<=x): print(ct2, end=“\t") #prints a tab after every ct2 ct2 += 1 ct += 1 print("\n") #this is a line break (starts a new line) return h(5)

22 What do we get? def g(a,b): while (a != b): while (a > b): a = a-b while(b>a): b = b-a return(a) print(g(6,20))


Download ppt "Strings: What can we do with Strings?"

Similar presentations


Ads by Google