Monday, April 27, 2009

Python string printing

Learning basic Python, it is a new task I am starting, I want to learn myself some basic Python programming. I have already knowledge of nuerous other programming language and python is in basic skills one of them. However, this evening something new happend, my girlfriend started to skim my books and started to read one of the Linux books I have. She is already for some time a Linux user however only on the GUI side and now wanted to learn all about the system she started to love including how the system was buildup from the kernel and the way she was able to manipulate it. based upon the books we started a discussion about reading a computer book cover to cover. I stated that if she was going to finish a 600 page Linux book and work here way from cover to cover I would also start reading a computer book and finish it from cover to cover. My choice was a book on Python programming. As I will go along in the book I will blog about it.

So here is the first blogpost about me learning Python from reading the book cover to cover. I will tag those posts with "Python cover to cover" so you can keep up. And please do remember, nothing is to basic to blog about because there is always someone who is not knowing what you write down.

First part, basic string printing. As for most languages Python has a function called print to write output to your screen. In our case it will write a string value to the screen, so if I want to print the text This is a test. to the screen in Python I will use print "This is a test." with the result as can seen below:
So this is quite simple, now we can do some basic manipulations on the printing of the text, for example let say that behind the text we would like to have a tab and then some more text. this can be done with the escape character of Python \ and the command for a tab a t. So we will be using print "This is a test.\tSome more text." or even a couple of \t commands.

As we now understand the tab command we can also use the command for a new line which is \n which will give us a new line. so for example if we want to have a This is a new text. and on a second line the text "This is text on a new line" we will be using the command print "This is a newtext.\nThis is text on a new line"

No comments: