So far we've done a lot of work setting up Python and only running some basic commands to test everything is running fine. In this post we are going to take our first real steps in using Python to actually doing something a little more interesting.
In this article we are going to do our best to start working with files. I find this to be a create place to start. If we can start reading what is in file as well as writing data to files we are doing something a little tangible instead of simply printing data to a screen. If you're able to read files and write files, this is a great start to storing data somewhere other than in the computer systems RAM. A text file can be stored on a systems hard drive to be used whenever the user needs it. We do both the reading and the writing with the open() Python function that comes standard with our installation.
We'll also introduce you to strings. Python uses a number of datatypes and strings are a great way that Python uses and works with, of course, strings of text.
Most functions will perform an action, in the case of the print() function, it will print something to the screen. The open() function will open a file for you and will return a File Object. This file object can then be used to read the information inside the file, as well as manipulate the data inside it.
Let's get stuck in and see exactly how to use the open() function within our interactive shell. Start by creating a directory to work in, and then run the interpretive shell with the python3 command:
$ python3
Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Start by running the following command. As you can see we are using the open() function. Inside the brackets, we are wanting to open the "text.txt" file. If this file has not been created previously, this file will be created. The second parameter inside the open() function is the "w" which is the write option, that will allow us to write information to the file. The "f = " at the start of the line of code, which simply saves the File Object into a variable which we have named "f". We'll explain more about variables, hopefully in our next post.
>>> f = open("test.txt","w")
Now that we have the File Object stored in our variable, we can now perform changes to our file. Run the commands below which will use the "write" method to modify our text file further. After each line, press enter and you will see the number of characters you have entered:
>>> f.write("This is a test file")
19
>>> f.write("All of this will be written to our new file")
43
>>> f.write("All in one line")
15
>>> f.close()
Remember to always use the close() method when you have finished writing to the file. This will close the file off and free up your memory. Notice the last line we enter states "All in one line". This is because all the data will be writing on a single line. If we want to have our text written on different lines, we need to include the new line character of \n at the end of each line, but more of that later.
>>> f = open("test.txt", "r")
>>> f.read()
'This is a test fileAll of this will be written to our new fileAll in one line'
>>> f.close()
variable = open(filename, mode)
In Python, and unlike other programming languages there are no difference between using single and double quotes. If you want to see how the string looks, you use the print() function as we have been in our previous posts.
You can mix single and double quotes, as we can see below. If you have your string in single quotes, you can use double quotes within the string value, and vise versa. As you can see in the examples below, the quotes inside the string are simple printed to in the output:
>>> print('"This" is a string')
"This" is a string
>>> print("It's also a string")
It's also a string
You can also escape quotes when they are inside a string as mixing strings can also cause errors. You can also see that the backslash can also be used to escape special characters like newline(\n) and tab(\t):
>>> print('It\'s also a string')
It's also a string
>>> print("hi there\nhow are you?")
hi there
how are you?
>>> print('''This is a triple quoted string
... as you can see we can print on multiple
... lines, and is perfect for documentation''')
This is a triple quoted string
as you can see we can print on multiple
lines, and is perfect for documentation
>>> print("test"*3)
testtesttest
>>> print("test"+"string")
teststring
>>> print("string"[1])
t
>>> print("string"[2:4])
ri
>>> print("b" in "string")
False
>>> print("b" not in "string")
True
>>> print("Testing a new %s" % "string")
Testing a new string
In the examples below, we can see we have braces {} in our print() function, with then the replacement value contains either an index number or argument or name of the argument.. The first example uses numbers to swap around the "Tea" and "Coffee" values, the second option simply uses the position of the values as they are to be returned in the print() function.
The final example then provides a demonstration that assigns a value to tea and coffee and the value is then printed to the screen.
>>> print("{1} and {0}".format("Tea","Coffee"))
Coffee and Tea
>>> print("{} and {}".format("Tea","Coffee"))
Tea and Coffee
>>> print("{coffee} and {tea}".format(tea="Tea",coffee="Coffee"))
Coffee and Tea
We've covered a lot of information here and hope its been useful to get you a little further in using and coding with Python. We'll continue with more in the coming weeks looking at using variables in our code and starting to work with integer data types. Let me know if there are any issues in the code or if you have any questions.