Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # All the Ways to Print... plus a bit on strings... 2022/03/05
- # 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?"
- # STRING FORMAT METHOD
- myString = "My name is {}. How do you do?".format(x)
- # F STRINGS
- myString = f"My name is {x}. How do you do?"
- print(myString)
- import math
- print("Pi to 2 places is {:.2f}".format(math.pi))
- print("Pi to 5 places is {:.5f}".format(math.pi))
- # padding to a length
- # "{:12}" # pad string out to 12 places
- # Let's talk about the print function
- # print with "no arguments"
- print() # newline... \n
- # print() with one argument
- print("Simple, one argument print call.")
- print(x)
- # print() has 2 optional keyword args
- print(end="\n") # end, default value of newline
- print(sep=" ") # sep, default of " "
- # print() with multiple args
- print("Hello", "I'm", "Johnny", "Cash") # sep=" "
- print("Hello", "I'm", "Johnny", "Cash", sep="...")
- print("Hello", "I'm", "Johnny", "Cash", end=" ")
- print()
- print("Hey!!!")
- # ANYTIME YOU TIME OVERRIDE END... do a "regular" call to print() with \n afterwards
- #... Like they say in Ch 2.9
- myString = "abc"
- myNextString = "123"
- print("{}{}".format(myString, myNextString))
- # \n \r \t \f " "
- print("In a hole in the ground there \r lived a Hobbit.") # prints " lived a Hobbit."
- myString = "Hey\r"
- myString = myString.rstrip()
- print(myString)
- myVar = input().rstrip() # same as myString.rstrip()
- # and if you're recasting
- myNum = int(input()) # = float(input())
- # then...
- myNum = int(input().rstrip()) # don't do this int(input()).rstrip()... rstrip() isn't an int method, it's a str method
- # Questions...
- # more rstrip() please
- # K, let's do this in Lab 18.4
- muppets = {
- "Kermit": "the Frog"
- }
- myVar = input()
- print("Hi ho! {} {} here!".format(myVar, muppets[myVar]))
- # so I put a stray space in input: "Kermit ", would be same with "Kermit\r" etc...
- # Traceback (most recent call last):
- # File "main.py", line 5, in <module>
- # print("Hi ho! {} {} here!".format(myVar, muppets[myVar]))
- # KeyError: 'Kermit '
- #... good error! "Kermit" is a key but "Kermit " isn't
- # I should have really used an IF statement and check to see
- # if that key is present in dict anyway before proceeding...
- muppets = {
- "Kermit": "the Frog"
- }
- myVar = input().rstrip() # fixed!
- print("Hi ho! {} {} here!".format(myVar, muppets[myVar]))
- # all good...
- # Hi ho! Kermit the Frog here!
- # and a look at Lab 5.14 where we build up some strings and print incrementally...
- highway_number = int(input().rstrip()) # not needed here, but doesn't hurt either!
- ''' Type your code here. '''
- # weed out invalid numbers first...
- if highway_number < 1 or highway_number > 999 or highway_number % 100 == 0: # invalid
- print("{} is not a valid interstate highway number.".format(highway_number))
- else: # so valid...
- if highway_number > 99: # aux
- print("I-{} is auxiliary".format(highway_number), end="") # empty string end
- # get the primary and tack it on
- primary_number = highway_number % 100
- print(", serving I-{}".format(primary_number), end="")
- else: # primary
- print("I-{} is primary".format(highway_number), end="") # we overrode the end \n a lot here!!!
- # handle the direction
- if highway_number % 2 ==0: # even
- print(", going east/west.") # we end up calling print() with a plain old hidden end="\n" either way...
- else: # odd
- print(", going north/south.") # ... so no need for a blank print() call on this one
Add Comment
Please, Sign In to add comment