module 5 strings and text files
TRANSCRIPT
OutlineCharacters and Strings
Text Files
Module 5Strings and Text Files
Tamer Abdou, PhD
IND 830 - Python Programming for Data Science
Data Science Laboratory
Ryerson University
Tamer Abdou, PhD Module 5: Control Statements 1/22
OutlineCharacters and Strings
Text Files
Outline
1 Characters and Strings
2 Text Files
Tamer Abdou, PhD Module 5: Control Statements 2/22
OutlineCharacters and Strings
Text Files
Accessing Characters and SubstringsStrings and Number SystemsString Methods
Characters and Strings
1 Characters and StringsAccessing Characters and SubstringsStrings and Number SystemsString Methods
2 Text Files
Tamer Abdou, PhD Module 5: Control Statements 3/22
OutlineCharacters and Strings
Text Files
Accessing Characters and SubstringsStrings and Number SystemsString Methods
The Structure of Strings
• A string is a data structure that consists of several smaller pieces of data
• A string is a sequence of zero or more characters
• The positions of those characters are numbered from 0 to the length of the stringminus one
• The len() function can be used to return the number of characters a stringcontains
Tamer Abdou, PhD Module 5: Control Statements 4/22
OutlineCharacters and Strings
Text Files
Accessing Characters and SubstringsStrings and Number SystemsString Methods
The Subscript Operator
<a s t r i n g > [<an i n t e g e r e x p r e s s i o n >]
• The first part is the inspected string
• The second part in brackets indicates the position of a particular character in theinspected string
• The integer expression is also called an index
Tamer Abdou, PhD Module 5: Control Statements 5/22
OutlineCharacters and Strings
Text Files
Accessing Characters and SubstringsStrings and Number SystemsString Methods
Example 5.1 (Script)
1 courseCode = "CIND 830"
2 #Display the length of the string which is '8'
3 print(len(courseCode))
4 #Display the 1st character which is 'C'
5 print(courseCode[0])
6 #Display the 7th character which is '3'
7 print(courseCode[6])
8 #Display the last character which is '0'
9 print(courseCode[len(courseCode)-1])
10 print(courseCode[-1])
Tamer Abdou, PhD Module 5: Control Statements 6/22
OutlineCharacters and Strings
Text Files
Accessing Characters and SubstringsStrings and Number SystemsString Methods
Slicing Strings
• A subscript is a portion of a string that can be extracted
• Python’s subscript operator can be used to obtain a substring through a processcalled ’slicing’
• To extract a substring, a colon ’:’ is used and an integer value can appear oneither side of the colon
• If two integer positions are included in the slice (e.g. [x : y ])• The range extends from x up to y − 1• If x is omitted, all the characters extending to the beginning will be included in
the substring• If y is omitted, all the characters extending to the end will be included in the
substring
Tamer Abdou, PhD Module 5: Control Statements 7/22
OutlineCharacters and Strings
Text Files
Accessing Characters and SubstringsStrings and Number SystemsString Methods
Example 5.2 (Script)
1 progLang = "Python"
2 #Display the first two characters which are "Py"
3 print(progLang[ 0 : 2 ])
4 print(progLang[ : 2 ])
5 print(progLang[ :-4 ])
6 #Display the last four characters which are "thon"
7 print(progLang[ 2 : 6 ])
8 print(progLang[ 2 : ])
9 print(progLang[-4 : ])
10 #Display all the characters
11 print(progLang[ 0 : 6 ])
12 print(progLang[ : 6 ])
13 print(progLang[ 0 : ])
14 print(progLang[ : ])
Tamer Abdou, PhD Module 5: Control Statements 8/22
OutlineCharacters and Strings
Text Files
Accessing Characters and SubstringsStrings and Number SystemsString Methods
Strings and Number Systems
• Any binary number can be represented by a string of bits
• To determine the integer quantity that a string of bits represents, we multiply thevalue of each bit (0 or 1) by its positional value, then add the results
• 1012 = 1 ∗ 22 + 0 ∗ 21 + 1 ∗ 20
• 10112 = 1 ∗ 23 + 0 ∗ 22 + 1 ∗ 21 + 1 ∗ 20
Tamer Abdou, PhD Module 5: Control Statements 9/22
OutlineCharacters and Strings
Text Files
Accessing Characters and SubstringsStrings and Number SystemsString Methods
Example 5.3 (Script)
1 #Convert 1011 to integer
2 bitString = "1011"
3 result = 0
4 exponent = len(bitString) - 1
5 for bit in bitString:
6 result = result + int(bit) * 2 ** exponent
7 exponent = exponent - 1
8 print("The integer value is", result)
Tamer Abdou, PhD Module 5: Control Statements 10/22
OutlineCharacters and Strings
Text Files
Accessing Characters and SubstringsStrings and Number SystemsString Methods
Example 5.3 (Program Execution)
(drag lower right corner to resize code editor)
Objects
str"101"
str"1"
int5
int-1
This mode is experimental. Use the regular Python Tutor to get live help and use more features.
Write code in Python 3.6
line that has just executed next line to execute
<< First < Back Done running (14 steps) Forward > Last >>
Help improve this tool by clicking whenever you learn something:
I just cleared up a misunderstanding! I just fixed a bug in my code!
Print output (drag lower right corner to resize)
Frames
Global frame
bitString
result
exponent
bit
show exited frames (Python) render all objects on the heap (Python) draw pointers as arrows [default]
Generate permanent link
Click the button above to create a permanent link to your visualization (video demo).
This live programming mode of Python Tutor (code on GitHub) supports three languages: Python 2.7 and 3.6 withlimited module imports, and JavaScript running in Node.js v6.0.0 with limited support for ES6. Try the regularPython Tutor visualizer for additional language support.
Privacy Policy: By using Python Tutor, your visualized code, options, user interactions, text chats, and IP addressare logged on our server and may be analyzed for research purposes. Nearly all web services collect this basicinformation from users in their server logs. However, Python Tutor does not collect any personally identifiableinformation from its users. It uses Google Analytics for website analytics.
Terms of Service: The Python Tutor service is provided for free on an as-is basis. Use this service at your own risk.Do not use it to share confidential information. The developers of Python Tutor are not responsible for the chatmessages or behaviors of any of the users on this website. We are also not responsible for any damages caused byusing this website. Finally, it is your responsibility to follow appropriate academic integrity standards.
Copyright © Philip Guo. All rights reserved.
('The integer value is', 5)
#Convert 101 to integerbitString = "101"result = 0exponent = len(bitString) - 1for bit in bitString: result = result + int(bit) * 2 ** exponent exponent = exponent - 1print("The integer value is", result)
12345678
Tamer Abdou, PhD Module 5: Control Statements 11/22
OutlineCharacters and Strings
Text Files
Accessing Characters and SubstringsStrings and Number SystemsString Methods
String Methods
<an object>.<method name>(<argument 1>, . . . , <argument n>)
• A method can be invoked by an object
• The object needs to be placed before the name of the method
• Methods can have arguments and return values. For example:• The split() method creates a list of words based on a specified separator
• The isalpha() returns True if the sentence contains only letters or False otherwise
• The upper() method returns an uppercase version of a given sentence or string
• The count() method returns the total number of non-overlapping occurrences of agiven substring
Tamer Abdou, PhD Module 5: Control Statements 12/22
OutlineCharacters and Strings
Text Files
Accessing Characters and SubstringsStrings and Number SystemsString Methods
Example 5.4 (Script)
1 #Practicing String Methods
2 Sentence = '''String literals are written in a variety of ways:
3 Single, Double, and Triple quotes'''
4
5 print(Sentence.split()) #Split without a delimiter
6 print(len(Sentence.split()))
7
8 print(Sentence.split("\n")) #Split using a delimiter
9 print(len(Sentence.split(":")))
10
11 print(Sentence.upper()) #Uppercase or lowercase
12 print(Sentence.lower())
13
14 print(Sentence.isalpha()) #Letters or Digits?
15 print(Sentence.isdigit())
16
17 print(Sentence.count("le")) #Number of occurrences of "le"
Tamer Abdou, PhD Module 5: Control Statements 13/22
OutlineCharacters and Strings
Text Files
Accessing Characters and SubstringsStrings and Number SystemsString Methods
Example 5.4 (Program Execution)(drag lower right corner to resize code editor)Write code in Python 3.6
Print output (drag lower right corner to resize)
['String', 'literals', 'are', 'written', 'in', 'a', 'variety', 'of', 'ways:', 'Single,', 'Double,', 'and', 'Triple', 'quotes'] 14['String literals are written in a variety of ways:', 'Single, Double, and Triple quotes']2STRING LITERALS ARE WRITTEN IN A VARIETY OF WAYS:SINGLE, DOUBLE, AND TRIPLE QUOTESstring literals are written in a variety of ways:single, double, and triple quotesFalseFalse3
#Practicing String MethodsSentence = '''String literals are written in a variety of ways:Single, Double, and Triple quotes'''
print(Sentence.split()) #Split without a delimiterprint(len(Sentence.split()))
print(Sentence.split("\n")) #Split using a delimiterprint(len(Sentence.split(":")))
print(Sentence.upper()) #Uppercase or lowercaseprint(Sentence.lower())
print(Sentence.isalpha()) #Letters or Digits?print(Sentence.isdigit())
print(Sentence.count("le")) #Number of occurrences of "le"
123456789
1011121314151617
Objects
str"String literals are written in a variety of ways:Single, Double, and Triple quotes"
Frames
Global frame
Sentence
Tamer Abdou, PhD Module 5: Control Statements 14/22
OutlineCharacters and Strings
Text Files
Reading and Writing FilesAccessing and Manipulating Files
Text Files
1 Characters and Strings
2 Text FilesReading and Writing FilesAccessing and Manipulating Files
Tamer Abdou, PhD Module 5: Control Statements 15/22
OutlineCharacters and Strings
Text Files
Reading and Writing FilesAccessing and Manipulating Files
Reading/Writing Text from/to a File
• Data can be input or output to a text file using a file object
• The open() function opens a connection to a specific file with an adjusted mode
parameter
• The mode parameter ’r’ is for input/reading files and ’w’ for output/writing files
• String data can be read using the read() method, and written using the write()
method
• Any opened file should be closed after finishing writing the data in it, and thatcan be achieved using the close() method
• Failure to close an opened file may result in data loss
Tamer Abdou, PhD Module 5: Control Statements 16/22
OutlineCharacters and Strings
Text Files
Reading and Writing FilesAccessing and Manipulating Files
Example 5.5 (Script)
1 #Text file Operations and Methods
2
3 textFile = open("myfile.txt", "w") #Opening a file for writing
4 textFile.write("First line.\nSecond line.\n")
5 textFile.close()
6
7 textFile = open("myfile.txt", "a") #Opening a file for appending
8 text = ["Third line.\n"]
9 textFile.writelines(text)
10 textFile.close()
11
12 textFile = open("myfile.txt", "r") #Opening a file for reading
13 contentsFile = textFile.read() #Read and Store
14 print(contentsFile) #Display the contents
15 textFile.close()
Tamer Abdou, PhD Module 5: Control Statements 17/22
OutlineCharacters and Strings
Text Files
Reading and Writing FilesAccessing and Manipulating Files
Reading/Writing Numbers from/to a File
• All file input and output operations return data as strings
• Functions int() and float() are used to convert data items to integers andfloating-point numbers respectively
• A simple for-loop can be used to access one line of text at a time
• A programmer can use the split() method to obtain a list of substrings thenconvert them into either integers or floating-point numbers
Tamer Abdou, PhD Module 5: Control Statements 18/22
OutlineCharacters and Strings
Text Files
Reading and Writing FilesAccessing and Manipulating Files
Accessing Files on Disks
• When interacting with files and before attempting to open them, it is preferableto check the existence of those files with the given path names
• The file system function os.path.exists() can be used to check the existenceof a file
• To return or change the path of the current working directory, the getcwd() orchdir(path) function can be used
• The mkdir() function is used to create folders, and the rmdir() function is usedto remove folders
Tamer Abdou, PhD Module 5: Control Statements 19/22
OutlineCharacters and Strings
Text Files
Reading and Writing FilesAccessing and Manipulating Files
Example 5.6 (Script)
1 #Accessing and Maniplulating Files
2
3 import os #import the OS library
4
5 currentDir = os.getcwd() #acquiring the current working directory
6
7 listOfFiles = os.listdir(currentDir) #Store the file names as a list
8
9 for name in listOfFiles:
10 if ".py" in name:
11 print(name)
Tamer Abdou, PhD Module 5: Control Statements 20/22
OutlineCharacters and Strings
Text Files
Reading and Writing FilesAccessing and Manipulating Files
Example 5.6 (Control Flow)
Accessing and Maniplulating Files
module
Shebang line: not specifiedEncoding: not specified
os import the OS library
currentDir = os.getcwd() acquiring the current working directory
listOfFiles = os.listdir(currentDir) Store the file names as a list
for
name in listOfFiles
Y".py" in name
print(name)
Tamer Abdou, PhD Module 5: Control Statements 21/22
OutlineCharacters and Strings
Text Files
Reading and Writing FilesAccessing and Manipulating Files
References
• Lambert, K. A. (2010). Strings and text files. In Fundamentals of Python: Fromfirst programs through data structures (pp. 121-157). Boston, MA: CengageLearning
• Python Software Foundation. (2020). Python 3.7.8 documentation. Retrievedfrom https://docs.python.org/3.7/index.html
Tamer Abdou, PhD Module 5: Control Statements 22/22