Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # We once called it "All the Ways to Print"... but printing isn't all we do to strings.
- # So we changed it to "String Manipulation and Printing"
- # but now it's
- # "Your Strings Are Jack Tripper"
- name = "Sue"
- # different way to build up a longer string
- # Concatenation with +
- myString = "My name is " + str(name) + ", how do you do?"
- print(myString)
- print("My name is " + name + ", how do you do?")
- # "data conversion specifiers" or "string modulo" %
- print("My name is %s, how do you do?" % (name))
- # str method .format()
- print("My name is {}, how do you do?".format(name))
- # f strings
- print(f"My name is {name}, how do you do?")
- import math
- print(math.pi)
- print("Pi to 3 decimal places is {:.3f}".format(math.pi))
- print(f"Pi to 3 decimal places is {math.pi:.3f}")
- print("John", "Paul", "George", "Ringo")
- # myString = f"{} {} {} {}"
- print("John", "Paul", "George", "Ringo", end="", sep="-->") # default value of end is "\n", sep is " "
- print() # if you use END, wrap with a call to print() with \n
- print("George M", "Billy P", "Pattie", "Yoko")
- # One of the 2 big mistakes I see with strings...
- # BIG MISTAKE #1 !!!
- # if you override END, always do a clean print() after, as mentioned in ZyBooks 2.9
- # a new clean line after using print(end="whatever") is expected. If the Labs or exam
- # ever vary from this, and it's rare, it's explicitly stated in the question (such as Lab 9.17).
- # BIG MISTAKE #2 !!!
- # The other biggest mistake?
- # Not using strip() on inputs!
- # The input() function returns a str...
- # Know your WHITESPACE
- " " # a space, from hitting the spacebar
- # There are also about 20-25 other simple spaces in Unicode
- "\n" # new line return
- "\r" # carriage return, back to beginning of the current line
- "\t" # tab
- "\b" # backspace
- "\f" # form feed
- # myVar = input().strip()
- muppetDict = {
- "Kermit": "the Frog",
- "Fozzy": "the Bear"
- }
- myVar = "Kermit " # "Kermit" is a dict key, but "Kermit ", "Kermit\r", etc ARE NOT
- myVar = myVar.strip()
- if myVar in muppetDict:
- print("Kermit is in the dictionary.")
- else:
- print("Nope. Not there.")
- # strip when recasting
- myVar = int(input().strip())
- myVar = float(input().strip())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement