strings ii review
play

Strings II Review Strings are stored character by - PowerPoint PPT Presentation

Strings II Review Strings are stored character by character. Can access each character individually by using an index:


  1. Strings ¡II ¡

  2. Review ¡ • Strings ¡are ¡stored ¡character ¡by ¡character. ¡ • Can ¡access ¡each ¡character ¡individually ¡by ¡ using ¡an ¡index: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡ ¡ ¡ ¡ ¡1 ¡ ¡ ¡ ¡ ¡ ¡2 ¡ ¡ ¡ ¡ ¡ ¡ ¡3 ¡ ¡ ¡ ¡ ¡ ¡4 ¡ ¡ ¡ ¡ ¡ ¡5 ¡ ¡ ¡ ¡ ¡ ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡7 ¡ "C" ¡ "o" ¡ "m" ¡ "p" ¡ "u" ¡ "t" ¡ "e" ¡ "r" ¡

  3. New ¡ • NegaDve ¡indexing ¡can ¡be ¡used. ¡ ¡ (Par%cularly ¡ useful ¡for ¡ge0ng ¡characters ¡near ¡the ¡end ¡of ¡a ¡ string.) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡ ¡ ¡ ¡ ¡1 ¡ ¡ ¡ ¡ ¡ ¡2 ¡ ¡ ¡ ¡ ¡ ¡ ¡3 ¡ ¡ ¡ ¡ ¡ ¡4 ¡ ¡ ¡ ¡ ¡ ¡5 ¡ ¡ ¡ ¡ ¡ ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡7 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑8 ¡ ¡ ¡ ¡ ¡-­‑7 ¡ ¡ ¡ ¡ ¡-­‑6 ¡ ¡ ¡ ¡ ¡-­‑5 ¡ ¡ ¡ ¡ ¡-­‑4 ¡ ¡ ¡ ¡ ¡-­‑3 ¡ ¡ ¡ ¡ ¡-­‑2 ¡ ¡ ¡ ¡-­‑1 ¡ "C" ¡ "o" ¡ "m" ¡ "p" ¡ "u" ¡ "t" ¡ "e" ¡ "r" ¡

  4. The ¡basic ¡string ¡for ¡loop ¡ • Use ¡this ¡whenever ¡you ¡need ¡to ¡process ¡a ¡ string ¡one ¡character ¡at ¡a ¡Dme. ¡ ¡ # assume s is a string variable for pos in range(0, len(s)): # do something with s[pos]

  5. s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a": total = total + 1 0 1 2 3 4 5 ¡ "b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡

  6. s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a": total = total + 1 pos ¡ 1st ¡itera1on ¡ pos: ¡0 ¡ 0 1 2 3 4 5 s[pos]: ¡"b" ¡ ¡ "b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡ total: ¡0 ¡ s[pos] ¡

  7. s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a": total = total + 1 pos ¡ 2 nd ¡itera1on ¡ pos: ¡1 ¡ 0 1 2 3 4 5 s[pos]: ¡"a" ¡ ¡ "b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡ total: ¡1 ¡ s[pos] ¡

  8. s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a": total = total + 1 pos ¡ 3 rd ¡itera1on ¡ pos: ¡2 ¡ 0 1 2 3 4 5 s[pos]: ¡"n" ¡ ¡ "b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡ total: ¡1 ¡ s[pos] ¡

  9. s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a": total = total + 1 pos ¡ 4 th ¡itera1on ¡ pos: ¡3 ¡ 0 1 2 3 4 5 s[pos]: ¡"a" ¡ ¡ "b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡ total: ¡2 ¡ s[pos] ¡

  10. s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a": total = total + 1 pos ¡ 5 th ¡itera1on ¡ pos: ¡4 ¡ 0 1 2 3 4 5 s[pos]: ¡"n" ¡ ¡ "b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡ total: ¡2 ¡ s[pos] ¡

  11. s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a": total = total + 1 pos ¡ 6 th ¡itera1on ¡ pos: ¡5 ¡ 0 1 2 3 4 5 s[pos]: ¡"a" ¡ ¡ "b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡ total: ¡3 ¡ s[pos] ¡

  12. Algorithm ¡-­‑> ¡FuncDon ¡ • CounDng ¡the ¡number ¡of ¡a ¡certain ¡character ¡in ¡ a ¡string ¡seems ¡like ¡a ¡good ¡candidate ¡for ¡a ¡ funcDon. ¡ def count_a(s): total = 0 for pos in range(0, len(s)): if s[pos] == "a" total = total + 1 return total ¡

  13. def count_a(s): total = 0 for pos in range(0, len(s)): if s[pos] == "a": total = total + 1 return total def main(): name = input("What is your name? ") freq = count_a(name) print("Your name has", freq, "A's in it.") ¡

  14. • Step ¡1: ¡Change ¡the ¡count ¡funcDon ¡so ¡it ¡takes ¡a ¡ second ¡argument ¡called ¡ letter . ¡ ¡The ¡funcDon ¡ should ¡count ¡the ¡number ¡of ¡Dmes ¡that ¡ letter occurs ¡in ¡the ¡string ¡(instead ¡of ¡only ¡lowercase ¡a's). ¡ • Step ¡2: ¡Change ¡the ¡main ¡funcDon ¡so ¡that ¡the ¡user ¡ can ¡type ¡in ¡their ¡name ¡and ¡a ¡leVer ¡and ¡the ¡ program ¡prints ¡the ¡frequency ¡of ¡that ¡leVer ¡in ¡their ¡ name. ¡ • Challenge : ¡Write ¡a ¡funcDon ¡count_dups ¡that ¡ counts ¡(and ¡returns) ¡all ¡occurrences ¡of ¡ consecuDve ¡duplicated ¡leVers ¡in ¡a ¡string. ¡ – e.g., ¡ count_dups("balloon") ¡returns ¡2. ¡

  15. Not ¡all ¡string ¡problems ¡are ¡solved ¡with ¡for ¡loops. ¡ def get_initial(firstname): first_init = firstame[0] return first_init

  16. String ¡ConcatenaDon ¡ • Combines ¡two ¡strings ¡into ¡a ¡new, ¡longer ¡string. ¡ • Uses ¡the ¡same ¡plus ¡sign ¡as ¡addiDon. ¡ s1 = "CS141" s2 = "rocks!" bigstring = s1 + s2 print(bigstring) # prints CS141rocks!

  17. String ¡ConcatenaDon ¡ • Unlike ¡print(), ¡string ¡concatenaDon ¡does ¡not ¡put ¡ spaces ¡between ¡your ¡strings. ¡ s1 = "CS141" s2 = "rocks!" bigstring = s1 + " " + s2 print(bigstring) # prints CS141 rocks!

  18. Sample ¡problem ¡ • All ¡professor ¡email ¡addresses ¡at ¡Rhodes ¡are ¡ constructed ¡from ¡the ¡professor's ¡last ¡name, ¡ followed ¡by ¡the ¡iniDal ¡leVer ¡of ¡their ¡first ¡ name. ¡ • We ¡want ¡to ¡design ¡a ¡funcDon ¡that ¡takes ¡a ¡ prof's ¡first ¡and ¡last ¡name ¡and ¡returns ¡their ¡ email ¡address. ¡

  19. def make_prof_email(first, last): init = first[0] address = last + init + "@rhodes.edu" return address def main(): firstname = input("First name: ") lastname = input("Last name: ") addr = make_prof_email(firstname, lastname) print("Email:", addr)

  20. You ¡try ¡it ¡ • Write ¡a ¡funcDon ¡ make_student_email ¡that ¡ creates ¡(and ¡returns) ¡a ¡student ¡email ¡address. ¡ • The ¡funcDon ¡should ¡take ¡four ¡parameters: ¡first ¡ name, ¡last ¡name, ¡middle ¡name, ¡and ¡class ¡year. ¡ • Challenge: ¡Modify ¡the ¡funcDon ¡so ¡it ¡takes ¡only ¡two ¡ parameters: ¡someone's ¡full ¡name ¡(one ¡string ¡with ¡ first, ¡middle, ¡and ¡last ¡names ¡within ¡it) ¡and ¡class ¡ year. ¡

  21. • A ¡fundamental ¡problem ¡when ¡using ¡strings ¡is ¡ compuDng ¡a ¡ substring , ¡or ¡a ¡string ¡ slice . ¡ • We ¡want ¡to ¡tell ¡Python ¡ – take ¡some ¡string, ¡ – give ¡me ¡all ¡the ¡characters ¡starDng ¡from ¡one ¡index, ¡ – and ¡ending ¡at ¡another ¡index. ¡ • Fortunately, ¡this ¡is ¡built ¡into ¡Python! ¡

  22. • Two ¡ways ¡to ¡use ¡square ¡brackets. ¡ • 1 ¡number ¡inside ¡the ¡brackets: ¡ – returns ¡ exactly ¡ one ¡character ¡of ¡a ¡string. ¡ – if ¡s ¡= ¡"Computer, ¡then ¡s[0] ¡returns ¡"C" ¡ • 2 ¡numbers ¡inside ¡the ¡brackets: ¡ – returns ¡a ¡ substring ¡or ¡string ¡ slice. ¡ ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend