Advertisement
jspill

webinar-strings-jack-tripper-2022-04-02.py

Apr 2nd, 2022
1,585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1. # We once called it "All the Ways to Print"... but printing isn't all we do to strings.
  2. # So we changed it to "String Manipulation and Printing"
  3. # but now it's
  4. # "Your Strings Are Jack Tripper"
  5.  
  6. # How We Build Strings
  7. # Concatenation - simple, but not the best! Too easy to make a mistake with spaces or punctuation
  8. name = "Sue"
  9. print("My name is " + str(name) + ", how do you do?") # this will wear you out in the long run
  10.  
  11. # Better!
  12. # string class format()
  13. print("My name is {}, how do you do?".format(name))
  14. # f strings
  15. print(f"My name is {name}, how do you do?")
  16.  
  17. # print()
  18. print() # 0 arguments to print... well except for the hidden
  19. print(end="\n")
  20. print("Hi.") # one argument
  21.  
  22. myList = [1, 2, 3, 4]
  23. for num in myList:
  24.     print(num)
  25.  
  26. # MISTAKE TO AVOID #1:
  27. # If you use the end param of print(), don't forget to make a clean line
  28. # see ZyBooks 2.9
  29. for num in myList:
  30.     print(num, end=" ") # any time we use end, we risk output on the same line later
  31. print() # the fix... make a blank call to print()
  32. print("the next thing I printed")
  33.  
  34. # Know your WHITESPACE
  35. " " # a space, from hitting the spacebar
  36. # There are also about 20-25 other simple spaces in Unicode
  37. "\n" # new line return
  38. "\r" # carriage return, back to beginning of the current line
  39. "\t" # tab
  40. "\f" # form feed
  41.  
  42. # MISTAKE TO AVOID #2:
  43. # when our unit testing gets stricter as the course progresses...
  44. # myVar = input()
  45. # myVar = myVar.strip() # string methods to get rid of "extra" whitespace: strip(), lstrip(), rstrip()
  46. # or do it in one step...
  47. # myVar = input().strip()
  48. #
  49. # myVar = int(input())
  50. # myVar = int(input().strip()) # though the int() function itself will ignore whitespace
  51.  
  52. muppetDict = {
  53.     "Kermit": "the Frog",
  54.     "Fozzy": "the Bear"
  55. }
  56. # Not keys: "Kermit\r", "Kermit\t", "Kermit ", etc
  57. myVar = "Kermit " # <-- not in the dictionary, but it will look much the same as "Kermit"
  58. myVar = myVar.strip() # get rid of stray whitespace characters
  59. if "Kermit" in muppetDict:
  60.     print("Kermit is in the dictionary.")
  61. else:
  62.     print("Nope. Not there.")
  63.  
  64. if myVar in muppetDict:
  65.     print("Kermit is in the dictionary.")
  66. else:
  67.     print("Nope. Not there.")
  68.  
  69. # STRINGS
  70. # be able to SLICE
  71. myString = "abcdef"
  72. # slice: myString[start:stop:step]
  73. revString = myString[::-1]
  74. print(revString) # fedbca
  75.  
  76. # Useful String Methods
  77. # myString.format()
  78. # myString.split() # makes a list of smaller strings
  79. # " ".join(myListOfStrings)
  80. # myString.strip() # lstrip(), rstrip()
  81. # myString.replace("old", "new") # myString.replace("old", "")
  82. # myString.count("subStr") # returns an int
  83. # myString.find("subStr") # return an int index of where it starts, -1 if not there
  84. # myString.isspace() # many "is" methods of the string type
  85. # myString.startswith("subStr")
  86.  
  87. # help(str)
  88. # print(dir(str))
  89. for item in dir(str):
  90.     if not item.startswith("_"):
  91.         print(item)
  92. # now that you're refreshed yourself with dir(), you can call help() more precisely:
  93. help(str.split)
  94.  
  95. # Don't assume you need a special module
  96. # Parsing 24 hour time as a string...
  97. myVar = "16:40"
  98. anotherTime = "9:30" # not zero padded
  99. yetAnotherTime = "09:30" # but here it is zero padded
  100. # ... so we don't know whether our hour will be 2 characters/indices or 1...
  101. timeList = yetAnotherTime.split(":")
  102. print(timeList) # ['09', '30']... hour and min!
  103.  
  104. # or slice it!
  105. # think you can't slice because the : isn't always in the same position?
  106. hour = anotherTime[:anotherTime.find(":")] # <- find() gets us around the problem
  107. print(hour)
  108.  
  109.  
  110.  
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement