More Printing with python

More Printing with python

Printing with python can be fun, their are lots of function you can use for printing. We will see few of them here and few in next chapter. Now we are going to do a bunch of exercises where you just type code in and make it run. I won’t be explaining this exercise because it is more of the same. The purpose is to build up your chops. See you in a few exercises, and do not skip! Do not paste!


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"
# Try removing end function from end and check output
print (end1 + end2 + end3 + end4 + end5 + end6, end=" ")
print (end7 + end8 + end9 + end10 + end11 + end12)

Time to Check the output

Now lets check what we did in above code:-

Line number 2

We can use “” or ‘ ‘ both to specify string, python consider both.

line number 4

What is this *10 for? This will print whatever is in “” for 10 times.

Line number  18

What is this end function?

Check the output end function specify what will be printed at the end of print function, by default it use “\n” but we can specify what to use. For this example we have specified to put a space at the end of print instead of moving it to next line.

For new Line

To specify a new line we can use \n which is knows as escape character. So lets take another example.

print ("this is first line\nThis is second \n We can use space before and after \\n, check starting of this line it have extra space. ")
I want you to check the output of this code by yourself and understand line \n output in third line.

But I have multiple line and it is not easy to manage with \n

In this case you can use “”” (Three time double-quote) How?

print ("""
Now this is my line 1
This is line 2
I can use \\n also here \nlike this

Some Other Escape Sequences

This is all of the escape sequences Python supports. You may not use many of these, but memorize their format and what they do anyway. Try them out in some strings to see if you can make them work. Don’t worry if you don’t understand all of them, I will explain as we use.

Escape What it does.
\\ Backslash (\)
\' Single-quote (‘)
\" Double-quote (“)
\a ASCII bell (BEL)
\b ASCII backspace (BS)
\f ASCII formfeed (FF)
\n ASCII linefeed (LF)
\N{name} Character named name in the Unicode database (Unicode only)
\r Carriage Return (CR)
\t Horizontal Tab (TAB)
\uxxxx Character with 16-bit hex value xxxx (u” string only)
\Uxxxxxxxx Character with 32-bit hex value xxxxxxxx (u” string only)
\v ASCII vertical tab (VT)
\ooo Character with octal value ooo
\xhh Character with hex value hh

Exercise of day (If you can’t then ask, its important to finish these exercise):

  • Try printing %s with print i.e print should be “I can print %s”.
  • Try to print variable with print in multiple line with help of Three time double-quote
  • Check Escape character \n , \t , \f , \r,\b, \v, \” , \’ and \\ (hint:- use some string without
  • Have FUN


no comments

Leave me comment