Advertisement
jspill

webinar-strings-jack-tripper-2022-05-07.py

May 7th, 2022
1,163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 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. name = "Sue"
  7. # different way to build up a longer string
  8.  
  9. # Concatenation with +
  10. myString = "My name is " + str(name) + ", how do you do?"
  11. print(myString)
  12. print("My name is " + name + ", how do you do?")
  13. # "data conversion specifiers" or "string modulo" %
  14. print("My name is %s, how do you do?" % (name))
  15.  
  16. # str method .format()
  17. print("My name is {}, how do you do?".format(name))
  18. # f strings
  19. print(f"My name is {name}, how do you do?")
  20.  
  21. import math
  22. print(math.pi)
  23. print("Pi to 3 decimal places is {:.3f}".format(math.pi))
  24. print(f"Pi to 3 decimal places is {math.pi:.3f}")
  25.  
  26. print("John", "Paul", "George", "Ringo")
  27. # myString = f"{} {} {} {}"
  28. print("John", "Paul", "George", "Ringo", end="", sep="-->") # default value of end is "\n", sep is " "
  29. print() # if you use END, wrap with a call to print() with \n
  30. print("George M", "Billy P", "Pattie", "Yoko")
  31.  
  32. # One of the 2 big mistakes I see with strings...
  33. # BIG MISTAKE #1 !!!
  34. # if you override END, always do a clean print() after, as mentioned in ZyBooks 2.9
  35. # a new clean line after using print(end="whatever") is expected. If the Labs or exam
  36. # ever vary from this, and it's rare, it's explicitly stated in the question (such as Lab 9.17).
  37.  
  38.  
  39. # BIG MISTAKE #2 !!!
  40. # The other biggest mistake?
  41. # Not using strip() on inputs!
  42. # The input() function returns a str...
  43.  
  44. # Know your WHITESPACE
  45. " " # a space, from hitting the spacebar
  46. # There are also about 20-25 other simple spaces in Unicode
  47. "\n" # new line return
  48. "\r" # carriage return, back to beginning of the current line
  49. "\t" # tab
  50. "\b" # backspace
  51. "\f" # form feed
  52. # myVar = input().strip()
  53.  
  54. muppetDict = {
  55.     "Kermit": "the Frog",
  56.     "Fozzy": "the Bear"
  57. }
  58. myVar = "Kermit " # "Kermit" is a dict key, but "Kermit ", "Kermit\r", etc ARE NOT
  59. myVar = myVar.strip()
  60. if myVar in muppetDict:
  61.     print("Kermit is in the dictionary.")
  62. else:
  63.     print("Nope. Not there.")
  64.  
  65. # strip when recasting
  66. myVar = int(input().strip())
  67. myVar = float(input().strip())
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement