# FramedStr.py # Examples to show how to display a string surrounded by a frame. # Examples build from simple and inflexible to more complicated but flexible. # Test as you go!!!! # Comment-out one line of code by putting a hash character (#) at the beginning of the line. """ Comment out multiple lines by enclosing the lines within triplets of quotation marks. """ #Better to select text and use Alt+3 to prepend a # character to each line and #use Alt+4 to remove the leading # character from selected lines. #Each of the examples below is self contained. #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #1 - Print a string: print("\nExample 1") #use \n to print the text following "\n" on a new line print("This is my string") #2 - Use asterisks (*) to surround the string with a box of characters print("\nExample 2") print("*******************") print("*This is my string*") print("*******************") #3 - Assign the string to a varible print("\nExample 3") myString = "This is my string" print("*******************") print("*" + myString + "*") print("*******************") #4 - Use the Len function to return the length of the string and # use it to adjust the width of the box. print("\nExample 4") myString = "This is my string" myStringLength = len(myString) #use the "len" function to get the length of myString print("*" * (myStringLength + 2)) # "a" * 5 produces a string of five "a" characters i.e. "aaaaa" print("*" + myString + "*") print("*" * (myStringLength + 2)) #5 - Modify #4 to draw a box around a string input by the user. # NOTE: The string could be of any length. # NOTE: Only the line assigning the string value to myString needs to be changed! print("\nExample 5") myString = input("Please enter a string: ") #ask user to enter any string they wish myStringLength = len(myString) print("*" * (myStringLength + 2)) print("*" + myString + "*") print("*" * (myStringLength + 2)) #6 - Modify #5 to add margins before and after the string. # Use variables so that you can change the width of the margins easily. print("\nExample 6") myString = input("Please enter a string: ") myStringLength = len(myString) leftMarginWidth = 2 #set a left margin of two characters rightMarginWidth = 2 #set a right margin of two characters MarginChr = " " #set the character to be used to show the margins leftMarginStr = MarginChr * leftMarginWidth #create a margin string rightMarginStr = MarginChr * rightMarginWidth #create a margin string print("*" * (myStringLength + 2 + leftMarginWidth + rightMarginWidth)) print("*" + leftMarginStr + myString + rightMarginStr + "*") print("*" * (myStringLength + 2 + leftMarginWidth + rightMarginWidth)) #7 - Modify #6 to use variables to show different characters for the box corner and lines. print("\nExample 7") myString = input("Please enter a string: ") myStringLength = len(myString) leftMarginWidth = 2 rightMarginWidth = 2 MarginChr = " " leftMarginStr = MarginChr * leftMarginWidth rightMarginStr = MarginChr * rightMarginWidth cornerChr = "+" # character to use for all four corners horizontalChr = "-" # character to use for horizontal lines verticalChr = "|" # character to use for vertical lines #print a corner character, horizontal characters to match the left margin, #the string, the right margin and another corner character print(cornerChr + horizontalChr * (myStringLength + leftMarginWidth + rightMarginWidth) + cornerChr) #print a vertical character, the left margin, the string, the right margin and a vertical character. print(verticalChr + leftMarginStr + myString + rightMarginStr + verticalChr) #print the bottom of the box (same as the top) print(cornerChr + horizontalChr * (myStringLength + leftMarginWidth + rightMarginWidth) + cornerChr) #8 - Modify #7 to use different Unicode characters for the box corners and lines. # There are approximately 300,000 Unicode characters identified with code point numbers. # They cover 168 modern and historical scripts, as well as multiple symbol sets. print("\nExample 8") myString = input("Please enter a string: ") myStringLength = len(myString) leftMarginWidth = 2 rightMarginWidth = 2 MarginChr = " " leftMarginStr = MarginChr * leftMarginWidth rightMarginStr = MarginChr * rightMarginWidth # assign unicode characters using the \u escape sequence, # followed by four hex digits giving the code point of the character. # These Unicode characters form a double-lined box topLeftChr = '\u2554' #╔ horizontalChr = '\u2550' #═ topRightChr = '\u2557' #╗ verticalChr = '\u2551' #║ bottomLeftChr = '\u255a' #╚ bottomRightChr = '\u255d' #╝ print(topLeftChr + horizontalChr * (myStringLength + leftMarginWidth + rightMarginWidth) + topRightChr) print(verticalChr + leftMarginStr + myString + rightMarginStr + verticalChr) print(bottomLeftChr + horizontalChr * (myStringLength + leftMarginWidth + rightMarginWidth) + bottomRightChr) #9 - Modify #8 to give the user a choice of box designs. print("\nExample 9") myString = input("Please enter a string: ") myStringLength = len(myString) #Invite the user to select a box style but only accept a valid integer answer. #Use a "while True" loop to ensure we get a valid input while True: try: boxStyle = int(input("Enter a number for a box style (0: simple, 1: single line, 2: double line): ")) except ValueError: #trap non-integer inputs e.g. letters or punctuation characters print("Please enter an integer value (0, 1 or 2)") continue if 0 <= boxStyle <= 2: break else: print("Please enter a valid number (0, 1 or 2)") #Why not just use: #boxStyle = int(input("Enter a number for a box style (0: simple, 1: single line, 2: double line): ")) #Try commenting out the while True loop (enclose in a pair of three " strings), #then uncomment the single line "boxStyle = int (..." and then enter incorrect values such as "a" or "4". #Continue with program leftMarginWidth = 2 rightMarginWidth = 2 MarginChr = " " leftMarginStr = MarginChr * leftMarginWidth rightMarginStr = MarginChr * rightMarginWidth #set appropriate boxStyle characters if boxStyle == 1: topLeftChr = '\u250c' #┌ horizontalChr = '\u2500' #─ topRightChr = '\u2510' #┐ verticalChr = '\u2502' #│ bottomLeftChr = '\u2514' #└ bottomRightChr = '\u2518' #┘ elif boxStyle == 2: topLeftChr = '\u2554' #╔ horizontalChr = '\u2550' #═ topRightChr = '\u2557' #╗ verticalChr = '\u2551' #║ bottomLeftChr = '\u255a' #╚ bottomRightChr = '\u255d' #╝ else: #boxStyle == 0 topLeftChr = '+' horizontalChr = '-' topRightChr = '+' verticalChr = '|' bottomLeftChr = '+' bottomRightChr = '*' print(topLeftChr + horizontalChr * (myStringLength + leftMarginWidth + rightMarginWidth) + topRightChr) print(verticalChr + leftMarginStr + myString + rightMarginStr + verticalChr) print(bottomLeftChr + horizontalChr * (myStringLength + leftMarginWidth + rightMarginWidth) + bottomRightChr) #10 - Modify #9 to use functions to create the strings to be printed. #Function to draw the top or bottom line of a box. def getHorizontalBorderStr(leftCornerChr, horizontalChr, rightCornerChr, leftMarginWidth, rightMarginWidth, strWidth): #use a backslash character (\) to continue a statement over more than one line return leftCornerChr + \ horizontalChr * (leftMarginWidth + strWidth + rightMarginWidth) + \ rightCornerChr #Function to draw a string between vertical box characters with margins def getBorderedStr(verticalChr, leftMarginWidth, rightMarginWidth, marginChr, myStr): #use a backslash character (\) to continue a statement over more than one line return verticalChr + \ marginChr * leftMarginWidth + \ myStr + \ marginChr * rightMarginWidth + \ verticalChr print("\nExample 10") myString = input("Please enter a string: ") myStringLength = len(myString) leftMarginWidth = 2 rightMarginWidth = 2 MarginChr = " " leftMarginStr = MarginChr * leftMarginWidth rightMarginStr = MarginChr * rightMarginWidth # assign unicode characters using the \u escape sequence, # followed by four hex digits giving the code point of the character # These Unicode characters form a double-lined box topLeftChr = '\u2554' #╔ horizontalChr = '\u2550' #═ topRightChr = '\u2557' #╗ verticalChr = '\u2551' #║ bottomLeftChr = '\u255a' #╚ bottomRightChr = '\u255d' #╝ #print(topLeftChr + horizontalChr * (myStringLength + leftMarginWidth + rightMarginWidth) + topRightChr) print(getHorizontalBorderStr(topLeftChr, horizontalChr, topRightChr, leftMarginWidth, rightMarginWidth, myStringLength)) #print(verticalChr + leftMarginStr + myString + rightMarginStr + verticalChr) print(getBorderedStr(verticalChr, leftMarginWidth, rightMarginWidth, MarginChr, myString)) #print(bottomLeftChr + horizontalChr * (myStringLength + leftMarginWidth + rightMarginWidth) + bottomRightChr) print(getHorizontalBorderStr(bottomLeftChr, horizontalChr, bottomRightChr, leftMarginWidth, rightMarginWidth, myStringLength)) """ Further challenges: 1) How would you modify this code to handle a list of strings entered by the user? TIP: You might need to find the longest entered string and pad other strings to the same length. 2) What changes would you need to make to left-align, centre or right-align the strings? TIP: Can you write a funcion to do these alignments? Does Python have built-in functions? 3) How would you allow for spaces between the lines? TIP: How could you adapt or use the existing functions to do this? 4) Could you combine all of these options into one function that could be stored in a separate file to create a library? 5) You could improve the program's readability by defining constants to identify the boxTypes e.g. bsCharacters = 0 bsSingleLine = 1 bsDoubleLine = 2 Assign a value to boxStyle e.g. boxStyle = bsDoubleLine Then check for the selected box style with if boxStyle == bsSingleLine: ...your code 6) What other ideas do you have to extend this? """