Manipulating Text Files And Strings In Beginner Python

avatar

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.

Reading And Writing Files With The Open Function


To read an write to files, as we said earlier we use the open() function. Functions are a block of predefined code within Python that can be called and run when needed. It's kind of like a program inside the programming language. There are many different functions built into Python. We mentioned the print() function with our last post, and you can even create your own functions, which we will look at in a later post.

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.

Reading The File


There's a little more to it, but we can use the open() function to read our file. Instead this time use the "r" parameter which of course means read. We can then use the read() method to read the contents of the file. As you can see below, we open the file and create the FIle Object storing it in the f variable, we then use the f.read() method to read the contents of the file and the of course close the File Object.

>>> 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()

NOTE:


If we used the open("test.txt", "w") again to write to the file again, we would remove all the existing data in the file. If we wanted to keep the rest of the data in place and add contents to the end of the file, we would use the "a" option to append.

Summing Up


Reading and writing to files using the open() function can be performed using the open() function passing in two parameters, one is the name of the file you want to use and the other is the mode, as we have seen so far, read(r), write(w) and append(a).

variable = open(filename, mode)

String Datatypes


In the work we've been doing so far with both the print() and open() functions, each time we have either been printing data or writing data we have been using String Datatypes. A String is simply a collection of one or more characters in between single, double or triple quotes.

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?

Triple Quoted Strings


As well as single and double quoted strings, we can also use triple quoted strings. As we can see below, this is perfect for printing our string across multiple lines. As you'll see later on, we can use triple quoted strings to document our code. The example below uses three single quotes but can be three double quotes as well. We have written across numerous lines to then print these same three lines as output.

>>> 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

String Operators


Python comes with a complete list operators that help you manipulate strings within your code. The following operators allow you to multiply strings, concatenate them(add together). You can see in the code below, the number in the square brackets after the string will print the reference to the number in the bracket, for example in our code below, string[1] will print the letter "t" as we start counting from 0 in Python. When we have two numbers joined by a colon, in our example string[2:4], this is called a slice and we get returned the values from reference 2 to 4.

>>> 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

Formatting Strings


So far we've been using the print() function to basically display our strings as they are. We can also expand the operations that the print() function performs to allow it to manipulate the strings we are using inside the brackets of the print() function. This string formatting is especially useful when we parse variables to the print() function. We can also format text we are printing directly in the function.

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.

Posted with STEMGeeks



0
0
0.000
0 comments