jspill

webinar-print-2021-07-10

Jul 10th, 2021 (edited)
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 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.  
  12. # hidden keyword arguments: end and sep
  13. # default end="\n" for a new line return
  14. # default sep=" "
  15. print("Hello.", "I'm", "Johnny", "Cash", ".", sep="") # Hello.I'mJohnnyCash.
  16. print(1, end="-")
  17. print(2, end="-")
  18. print(3)
  19.  
  20. x = "Sue"
  21.  
  22. # ways we build up larger strings
  23. # CONCATENATION with +
  24. myString = "My name is " + str(x) + ". How do you do?"
  25.  
  26. # DATA CONVERSION SPECIFIERS or "STRING MODULO" %s %f %d %i
  27. myString = "My name is %s. How do you do?" % (x)
  28.  
  29. # THE BEST WAY:  string class .format() method
  30. myString = "My name is {}. How do you do?".format(x)
  31.  
  32.  
  33. print(myString)
  34.  
  35. print("I bet there's rich folks {} in a fancy {}".format("eating", "dining car"))
  36.  
  37. # Use the given username and phone to create a message that lets the user know that you will be calling
  38. # at a specified number for your appointment. Use the format method to insert data into the printed message.
  39. # expected outcome:
  40. # Hi, Allen. I will call you at 888-555-0011 for our appointment.
  41. username = 'Allen'
  42. phone = 8885550011
  43.  
  44. # let's recast that int to a str, then we can SLICE it
  45. phone = str(phone)
  46.  
  47. # I'll write slices as variables so we can see them...
  48. first = phone[0:3]
  49. second = phone[3:6]
  50. last = phone[6:]
  51.  
  52. # ... but I usually don't bother to store things in variables unless they'll be used many times later on
  53.  
  54. userMessage = "Hi, {}. I will call you at {}-{}-{} for our appointment.".format(username, phone[0:3], phone[3:6], phone[6:])
  55. print(userMessage)
Add Comment
Please, Sign In to add comment