Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # All the Ways to Print 2021/11/06
- # the basic string object manipulation... and the print() function
- # Ways we build up larger strings
- x = "Sue" # a string!
- # CONCATENATION with +
- myString = "My name is " + str(x) + ". How do you do?" # all must be strings
- # DATA CONVERSION SPECIFIERS or "string modulo"
- myString = "My name is %s. How do you do?" % (x) # %i %d, %f
- # STRING FORMAT METHOD #........ BEST!
- myString = "My name is {}. How do you do?".format(x)
- # F STRINGS #........ also best!
- myString = f"My name is {x}. How do you do?"
- print(myString)
- # THE PRINT() FUNCTION
- # print() with no arguments
- print()
- # print() with one argument
- print("Simple, one argument print call.")
- print(x)
- # print() with multiple arguments
- print("Hello", "I'm", "Johnny", "Cash")
- print("Hello", "I'm", "Johnny", "Cash", end="\n", sep=" ")
- print("Hello", "I'm", "Johnny", "Cash", end=" ")
- # print() has optional, hidden arguments
- # hidden keyword arguments: end and sep
- # default end="\n" for a new line return
- # default sep=" "
- print("Hello", "I'm", "Johnny", "Cash", sep="--")
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
- for item in myList:
- print(item, end=" ")
- print() # get the line return again
- # for item in myList: # consider the length vs the index
- for i in range(len(myList)):
- if i != len(myList) - 1:
- print(myList[i], end=" ")
- else:
- print(myList[i])
- print("String join() method:")
- print(" ".join(myList))
- print(myString)
- print(myString.split())
- print(myList)
- # SLICES
- # a slice is an abbreviated copy of a string, list, even tuple
- print(myString[0:7])
- revString = myString[::-1]
- print(revString)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement