Python Strings,Text and Printing
Despite having experience developing Python strings, you are still unsure of their function. To help you understand what these variables are for, we’ve created a ton of them with intricate strings for this exercise. Python strings are first described.
What is Python Strings?
A python strings is typically some text that you want to “export” from the program you are building or display to someone. Python understands that you want something to be a string when you surround it with either ” (double-quotes) or'(single-quotes). When you use print and place the text you want to go to the string inside of ” or’after the print, you have probably seen this a lot. Python then outputs it.
The format characters you have discovered so far may be found in python strings. The formatted variables are simply added to the string, followed by the percent sign (%) and the variable. The only exception is that you must place your formats inside of () parentheses and separate them with commas if you want your string to output multiple variables using different formats. It’s like you told me what you wanted from the supermarket and said, “I want milk, eggs, bread, and soup.” Only in programming do we say, “(milk, eggs, bread, soup).”
We will now type in a whole bunch of strings, variables, and formats, and print them. You will also practice using short abbreviated variable names. Python Programmers love saving themselves time at your expense by using annoying cryptic variable names, so let’s get you started being able to read and write them early on.
x = "There are %d types of people." % 10 binary = "binary" do_not = "don't" y = "Those who know %s and those who %s." % (binary, do_not) print x print y print "I said: %r." % x print "I also said: '%s'." % y hilarious = False joke_evaluation = "Isn't that joke so funny?! %r" print joke_evaluation % hilarious w = "This is the left side of..." e = "a string with a right side." print w + e
Python Printing
We will now use Python printing to just type in code and let it run. We have already discussed this subject in detail because it is essentially the same.
print "Mary had a little lamb." print "Its fleece was white as %s." % 'snow' print "And everywhere that Mary went." print "." * 10 # what'd that do? end1 = "C" end2 = "h" end3 = "e" end4 = "e" end5 = "s" end6 = "e" end7 = "B" end8 = "u" end9 = "r" end10 = "g" end11 = "e" end12 = "r" # watch that comma at the end. try removing it to see what happens print end1 + end2 + end3 + end4 + end5 + end6, print end7 + end8 + end9 + end10 + end11 + end12
formatter = "%r %r %r %r" print formatter % (1, 2, 3, 4) print formatter % ("one", "two", "three", "four") print formatter % (True, False, False, True) print formatter % (formatter, formatter, formatter, formatter) print formatter % ( "I had this thing.", "That you could type up right.", "But it didn't sing.", "So I said goodnight."
Here's some new strange stuff, remember type it exactly. days = "Mon Tue Wed Thu Fri Sat Sun" months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" print "Here are the days: ", days print "Here are the months: ", months print """ There's something going on here. With the three double- quotes. We'll be able to type as much as we like. Even 4 lines if we want, or 5, or 6. """