Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # All the Ways to Print
- # the print() function and basic string object manipulation
- # print() with one argument
- print("Simple, one argument print call.")
- print() # or none, just for a line return
- # print() with multiple arguments
- print("Hello.", "I'm", "Johnny", "Cash")
- # hidden keyword arguments: end and sep
- # default end="\n" for a new line return
- # default sep=" "
- print("Hello.", "I'm", "Johnny", "Cash", ".", sep="") # Hello.I'mJohnnyCash.
- print(1, end="-")
- print(2, end="-")
- print(3)
- x = "Sue"
- # ways we build up larger strings
- # CONCATENATION with +
- myString = "My name is " + str(x) + ". How do you do?"
- # DATA CONVERSION SPECIFIERS or "STRING MODULO" %s %f %d %i
- myString = "My name is %s. How do you do?" % (x)
- # THE BEST WAY: string class .format() method
- myString = "My name is {}. How do you do?".format(x)
- print(myString)
- print("I bet there's rich folks {} in a fancy {}".format("eating", "dining car"))
- # Use the given username and phone to create a message that lets the user know that you will be calling
- # at a specified number for your appointment. Use the format method to insert data into the printed message.
- # expected outcome:
- # Hi, Allen. I will call you at 888-555-0011 for our appointment.
- username = 'Allen'
- phone = 8885550011
- # let's recast that int to a str, then we can SLICE it
- phone = str(phone)
- # I'll write slices as variables so we can see them...
- first = phone[0:3]
- second = phone[3:6]
- last = phone[6:]
- # ... but I usually don't bother to store things in variables unless they'll be used many times later on
- userMessage = "Hi, {}. I will call you at {}-{}-{} for our appointment.".format(username, phone[0:3], phone[3:6], phone[6:])
- print(userMessage)
Add Comment
Please, Sign In to add comment