jspill

webinar-print-2022-03-05

Mar 5th, 2022
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. # All the Ways to Print... plus a bit on strings... 2022/03/05
  2. # the basic string object manipulation... and the print() function
  3.  
  4. # Ways we build up larger strings
  5. x =  "Sue" # a string!
  6.  
  7. # CONCATENATION with +
  8. myString = "My name is " + str(x) + ". How do you do?"
  9. # STRING FORMAT METHOD
  10. myString = "My name is {}. How do you do?".format(x)
  11. # F STRINGS
  12. myString = f"My name is {x}. How do you do?"
  13. print(myString)
  14. import math
  15. print("Pi to 2 places is {:.2f}".format(math.pi))
  16. print("Pi to 5 places is {:.5f}".format(math.pi))
  17.  
  18. # padding to a length
  19. # "{:12}" # pad string out to 12 places
  20.  
  21. # Let's talk about the print function
  22. # print with "no arguments"
  23. print() # newline... \n
  24.  
  25. # print() with one argument
  26. print("Simple, one argument print call.")
  27. print(x)
  28.  
  29. # print() has 2 optional keyword args
  30. print(end="\n") # end, default value of newline
  31. print(sep=" ") # sep, default of " "
  32.  
  33. # print() with multiple args
  34. print("Hello", "I'm", "Johnny", "Cash") # sep=" "
  35. print("Hello", "I'm", "Johnny", "Cash", sep="...")
  36.  
  37. print("Hello", "I'm", "Johnny", "Cash", end=" ")
  38. print()
  39. print("Hey!!!")
  40. # ANYTIME YOU TIME OVERRIDE END... do a "regular" call to print() with \n afterwards
  41. #... Like they say in Ch 2.9
  42.  
  43. myString = "abc"
  44. myNextString = "123"
  45. print("{}{}".format(myString, myNextString))
  46.  
  47. # \n \r \t \f " "
  48. print("In a hole in the ground there \r lived a Hobbit.") # prints " lived a Hobbit."
  49.  
  50. myString = "Hey\r"
  51. myString = myString.rstrip()
  52. print(myString)
  53.  
  54. myVar = input().rstrip() # same as myString.rstrip()
  55. # and if you're recasting
  56. myNum = int(input()) # = float(input())
  57. # then...
  58. myNum = int(input().rstrip()) # don't do this int(input()).rstrip()... rstrip() isn't an int method, it's a str method
  59.  
  60. # Questions...
  61. # more rstrip() please
  62. # K, let's do this in Lab 18.4
  63. muppets = {
  64.     "Kermit": "the Frog"
  65.     }
  66. myVar = input()
  67. print("Hi ho! {} {} here!".format(myVar, muppets[myVar]))
  68.  
  69. # so I put a stray space in input: "Kermit ", would be same with "Kermit\r" etc...
  70. # Traceback (most recent call last):
  71. #   File "main.py", line 5, in <module>
  72. #     print("Hi ho! {} {} here!".format(myVar, muppets[myVar]))
  73. # KeyError: 'Kermit '
  74. #... good error! "Kermit" is a key but "Kermit " isn't
  75.  
  76. # I should have really used an IF statement and check to see
  77. # if that key is present in dict anyway before proceeding...
  78. muppets = {
  79.     "Kermit": "the Frog"
  80.     }
  81. myVar = input().rstrip() # fixed!
  82. print("Hi ho! {} {} here!".format(myVar, muppets[myVar]))
  83. # all good...
  84. # Hi ho! Kermit the Frog here!
  85.  
  86.  
  87. # and a look at Lab 5.14 where we build up some strings and print incrementally...
  88. highway_number = int(input().rstrip()) # not needed here, but doesn't hurt either!
  89.  
  90. ''' Type your code here. '''
  91. # weed out invalid numbers first...
  92. if highway_number < 1 or highway_number > 999 or highway_number % 100 == 0: # invalid
  93.     print("{} is not a valid interstate highway number.".format(highway_number))
  94. else: # so valid...
  95.     if highway_number > 99: # aux
  96.         print("I-{} is auxiliary".format(highway_number), end="") # empty string end
  97.         # get the primary and tack it on
  98.         primary_number = highway_number % 100
  99.         print(", serving I-{}".format(primary_number), end="")
  100.     else: # primary
  101.         print("I-{} is primary".format(highway_number), end="")  # we overrode the end \n a lot here!!!
  102.     # handle the direction
  103.     if highway_number % 2 ==0: # even
  104.         print(", going east/west.") # we end up calling print() with a plain old hidden end="\n" either way...
  105.     else: # odd
  106.         print(", going north/south.") # ... so no need for a blank print() call on this one
  107.  
  108.  
  109.  
  110.  
  111.  
Add Comment
Please, Sign In to add comment