# 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" # How We Build Strings # Concatenation - simple, but not the best! Too easy to make a mistake with spaces or punctuation name = "Sue" print("My name is " + str(name) + ", how do you do?") # this will wear you out in the long run # Better! # string class format() print("My name is {}, how do you do?".format(name)) # f strings print(f"My name is {name}, how do you do?") # print() print() # 0 arguments to print... well except for the hidden print(end="\n") print("Hi.") # one argument myList = [1, 2, 3, 4] for num in myList: print(num) # MISTAKE TO AVOID #1: # If you use the end param of print(), don't forget to make a clean line # see ZyBooks 2.9 for num in myList: print(num, end=" ") # any time we use end, we risk output on the same line later print() # the fix... make a blank call to print() print("the next thing I printed") # 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 "\f" # form feed # MISTAKE TO AVOID #2: # when our unit testing gets stricter as the course progresses... # myVar = input() # myVar = myVar.strip() # string methods to get rid of "extra" whitespace: strip(), lstrip(), rstrip() # or do it in one step... # myVar = input().strip() # # myVar = int(input()) # myVar = int(input().strip()) # though the int() function itself will ignore whitespace muppetDict = { "Kermit": "the Frog", "Fozzy": "the Bear" } # Not keys: "Kermit\r", "Kermit\t", "Kermit ", etc myVar = "Kermit " # <-- not in the dictionary, but it will look much the same as "Kermit" myVar = myVar.strip() # get rid of stray whitespace characters if "Kermit" in muppetDict: print("Kermit is in the dictionary.") else: print("Nope. Not there.") if myVar in muppetDict: print("Kermit is in the dictionary.") else: print("Nope. Not there.") # STRINGS # be able to SLICE myString = "abcdef" # slice: myString[start:stop:step] revString = myString[::-1] print(revString) # fedbca # Useful String Methods # myString.format() # myString.split() # makes a list of smaller strings # " ".join(myListOfStrings) # myString.strip() # lstrip(), rstrip() # myString.replace("old", "new") # myString.replace("old", "") # myString.count("subStr") # returns an int # myString.find("subStr") # return an int index of where it starts, -1 if not there # myString.isspace() # many "is" methods of the string type # myString.startswith("subStr") # help(str) # print(dir(str)) for item in dir(str): if not item.startswith("_"): print(item) # now that you're refreshed yourself with dir(), you can call help() more precisely: help(str.split) # Don't assume you need a special module # Parsing 24 hour time as a string... myVar = "16:40" anotherTime = "9:30" # not zero padded yetAnotherTime = "09:30" # but here it is zero padded # ... so we don't know whether our hour will be 2 characters/indices or 1... timeList = yetAnotherTime.split(":") print(timeList) # ['09', '30']... hour and min! # or slice it! # think you can't slice because the : isn't always in the same position? hour = anotherTime[:anotherTime.find(":")] # <- find() gets us around the problem print(hour)