Advertisement
jspill

webinar-print-2021-05-01

May 1st, 2021
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. # All the Ways to Print
  2. # the print() function and basic string object manipulation
  3.  
  4. # print() with one argument
  5. print("Simple, one argument print call.")
  6. print() # or none, just for a line return
  7.  
  8. # print() with multiple arguments
  9. print("Hello.", "I'm", "Johnny", "Cash.")
  10.  
  11. # hidden keyword arguments: end and sep
  12. print("Hello.", "I'm", "Johnny", "Cash", ".", sep="--", end="") # overrode sep=" " and end="\n" defaults
  13. print('Next line? No.')
  14.  
  15. # ways we build up larger strings
  16. # CONCATENATION + # easy to understand, also easy to mess up
  17. x ="Sue"
  18. print("My name is " + x + ". How do you do?") # if x wasn't a string, I'd recast it str(x)
  19. myString = "My name is " + x + ". How do you do?"
  20.  
  21. # data conversion specifiers or "string modulo" %s %f %d
  22. print("I bet there's rich folks %s and in a fancy %s." % ("eating", "dining car"))
  23.  
  24. # THE BEST WAY:  string class .format() method
  25. print("They're probably drinking {} and smoking big {}!".format("coffee", "CIGARS"))
  26.  
  27. phone = 5553337878
  28. phone = str(phone) # "5553337878"
  29. myString = "({}){}-{}".format(phone[0:3], phone[3:6], phone[6:])
  30. print(myString)
  31.  
  32. myList = ["Gilligan", "Scooby", "Agent Scully", "Fonzie"]
  33. print(myList) # that's a list with [ ]
  34. for item in myList:
  35.     if myList[-1] == item:
  36.         print(item)
  37.     else:
  38.         print(item, end=", ")
  39.  
  40. # for item in myList:
  41. #     print(item, sep=",", end=" ") # doesn't work here, no sep b/c each call to print() has 1 arg only
  42.  
  43. # print(dir(str)) # to see methods and properties of a type
  44. # help(str) # to see full help doc on type
  45. # help(str.format) # to see help on just one method
  46.  
  47. # myString.method() is the usual syntax, but with .join() the separator string is what you start with...
  48. print(", ".join(myList))
  49.  
  50. # help(str.join)
  51. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  52. print(", ".join(myTuple))
  53.  
  54. import random
  55. help(random.choice)
  56.  
  57.  
  58. import math
  59. # math.e or math.pi
  60. print(math.e)
  61.  
  62. # string modulo/data conversion specifier
  63. # 5 decimal places for a float --> %.5f
  64. print("math.e to 5 places is %.5f" % math.e)
  65.  
  66. # 5 decimal places for .format() --> :.5f
  67. print("math.e to 5 places is {:.5f}".format(math.e))
  68.  
  69. # Question: "Can you use string modulo and string .format() together
  70. # Answer: No reason to, as you'd normally pick one OR the other. But you could...
  71. # ... ponders stringly
  72. # someVar = "pl{}".format("aces")
  73. print("Pi to %d %s" % (5, "pl{}".format("aces")+"."))
  74. # even better:
  75. print("Pi to %d %s is %.5f%s" % (5, "pl{}".format("ac")+"es", math.pi, ".")) # inelegant, but it works!
  76. # This has been a weird thought experiment in using string .format(), string modulo, and concatenation at the same time
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement