Week 4 - Strings and Text Files
Week 4 - Strings and Text Files
FUNDAMENTALS
1-1
Outline
3. String Methods
4. Text Files
1-2
Structure of Strings
A string is a data structure : compound unit that consists of
several smaller pieces of data.
len function returns the length of a string
[Lambert, 2012]
1-5 1-5
Slicing for Substrings
substrings.: portions of strings. Operation of obtaining a
substring is slicing
Example 2:
Class Practice 1:
'p'
'e'
13
'myprogra'
1-7 1-7
Outline
3. String Methods
4. Text Files
1-8
Strings and Number Systems
decimal number system: based 10, use ten character 0 to 9.
Binary number system: based 2, use two character 0 and 1.
Octal number system: based 8, use two character 0 to 8.
Hexadecimal number system: based 16, use sixteen character 0
to 9 and A to F
1-9 1-9
Conversion:decimaltobinary
How to convert 35 in decimal to binary?
• Start with the number in decimal,
• Move to the left while continuously finding the
quotients and the remainder of division by 2. The
result is 35 = (100011)2.
(00101111)2
1-10
Conversion:binarytodecimal
How to convert decimal number 165 to binary?
• break the number as the sum of numbersthat are
equivalent to the binary place values shown:
(11001)2 2510
1-11
Conversion:decimaltohexadecimal
How to convert 126 in decimal to its equivalentin the
hexadecimal system?
• We move to the left while continuously finding the
quotients and the remainder of division by 16. The result
is 126 = (7E)16
(2F)16
1-12
Conversion:decimaltooctal
How to convert 126 in decimal to its equivalent in the octal
system?
• We move to the left while continuously finding the
quotients and the remainder of division by 8. The
result is 126 = (176)8
578
1-13
Conversion:binarytohexadecimal
Show the hexadecimal equivalent of the binary number
(110011100010)2.
Solution
We first arrange the binary number in 4-bit patterns:
1100 1110 0010
Note that the leftmost pattern can have one to four bits. We
then use the equivalent of each pattern shown in previous table
to change the number to hexadecimal: (CE2)16.
Class Practice 6: Convert binary numbers to hexadecimal:
(11001)2 1916
1-14
Conversion:hexadecimaltobinary
What is the binary equivalent of (24C)16?
Solution
Each hexadecimal digit is converted to 4-bit patterns:
1-15
decimaltobinaryprogram
Example 4:
Sample output:
1-16
binarytodecimalprogram
Example 5:
Sample output:
INTRODUCTION 1-17
Outline
3. String Methods
4. Text Files
1-18
String Methods
String method split to obtain a list of the words contained in
an input string.
Example 6:
1-
1-19 19
String Methods
1-
[Lambert, 2012] 1-20 20
String Methods
[Lambert, 2012]
1-
1-21 21
String Methods
String Method Examples
1-
1-22 22
Strings and Number Systems
Override default setting of split function
1-
1-23 23
Outline
3. String Methods
4. Text Files
1-24
Writing Text to a File
Data can be output to a text file using a file object
Example 7:
1-
1-25 25
Writing Numbers to a File
Example 8:
1-
1-26 26
ReadingText from a File
Assume that your current working directory contain the file
myfile.txt
1-
1-27 27
Reading Numbers from a File
Assume that your current working directory contain the file
integers.txt
Example 9:
1-
1-28 28
Reading Numbers from a File
Obtaining numbers from a text, separated by spaces.
Example 10:
1-
1-29 29
File Operations
[Lambert, 2012]
1-
1-30 30
File and Directory Operations
Print all of the names of files in the current working directory
Example 11:
1-
1-31 31
File and Directory Operations
[Lambert, 2012]
1-
1-32 32
REFERENCES
This lecture was developed based on Chapter 4 in:
1-33