Advertisement
jspill

webinar-print-2021-11-06-v4

Nov 6th, 2021
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. # All the Ways to Print 2021/11/06
  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?" # all must be strings
  9. # DATA CONVERSION SPECIFIERS or "string modulo"
  10. myString = "My name is %s. How do you do?" % (x) # %i %d, %f
  11. # STRING FORMAT METHOD #........ BEST!
  12. myString = "My name is {}. How do you do?".format(x)
  13. # F STRINGS  #........ also best!
  14. myString = f"My name is {x}. How do you do?"
  15.  
  16. print(myString)
  17.  
  18. # THE PRINT() FUNCTION
  19.  
  20. # print() with no arguments
  21. print()
  22.  
  23. # print() with one argument
  24. print("Simple, one argument print call.")
  25. print(x)
  26.  
  27. # print() with multiple arguments
  28. print("Hello", "I'm", "Johnny", "Cash")
  29. print("Hello", "I'm", "Johnny", "Cash", end="\n", sep=" ")
  30. print("Hello", "I'm", "Johnny", "Cash", end=" ")
  31.  
  32. # print() has optional, hidden arguments
  33.  
  34. # hidden keyword arguments: end and sep
  35. # default end="\n" for a new line return
  36. # default sep=" "
  37.  
  38. print("Hello", "I'm", "Johnny", "Cash", sep="--")
  39.  
  40. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
  41. for item in myList:
  42.     print(item, end=" ")
  43.  
  44. print() # get the line return again
  45.  
  46. # for item in myList: # consider the length vs the index
  47. for i in range(len(myList)):
  48.     if i != len(myList) - 1:
  49.         print(myList[i], end=" ")
  50.     else:
  51.         print(myList[i])
  52.  
  53. print("String join() method:")
  54. print(" ".join(myList))
  55.  
  56. print(myString)
  57. print(myString.split())
  58. print(myList)
  59.  
  60. # SLICES
  61. # a slice is an abbreviated copy of a string, list, even tuple
  62. print(myString[0:7])
  63. revString = myString[::-1]
  64. print(revString)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement