0% found this document useful (0 votes)
5 views

Week 4 - Strings and Text Files

Uploaded by

ashish sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Week 4 - Strings and Text Files

Uploaded by

ashish sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

ICT701-PROGRAMMING

FUNDAMENTALS

Week 4 – Strings and Text Files

1-1
Outline

1. Accessing Characters and


Substrings

2. Strings and Number


Systems

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]

String is an immutable data structure: internal data


elements, the characters, can be accessed, but the structure
itself can not be modified. 1-3 1-3
Structure of Strings
Accessing String Elements

positions usually range from 0 to the length minus 1

[Lambert, 2012] 1-4 1-4


Structure of Strings
Example 1:

count-controlled loop to display the characters and their positions

1-5 1-5
Slicing for Substrings
substrings.: portions of strings. Operation of obtaining a
substring is slicing

Example 2:

[Lambert, 2012] 1-6 1-6


Slicing for Substrings
Testing for a Substring with the in Operator

Class Practice 1:

'p'
'e'
13
'myprogra'
1-7 1-7
Outline

1. Accessing Characters and


Substrings

2. Strings and Number


Systems

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

Subscript is used to identify the system.

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.

Class Practice 2: Convert decimal numbers to binary :

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

Class Practice 3: Convert binary numbers to decimal:

(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

Class Practice 4: Convert decimal numbers to hexadecimal:

(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

Class Practice 5: Convert decimal numbers to octal:

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:

2 → 0010, 4 → 0100, and C → 1100


The result is (001001001100)2.

Class Practice 7: Convert hexadecimal numbers to binary:

12716 (10010 0111)2

1-15
decimaltobinaryprogram
Example 4:

Sample output:

1-16
binarytodecimalprogram
Example 5:

Sample output:

INTRODUCTION 1-17
Outline

1. Accessing Characters and


Substrings

2. Strings and Number


Systems

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

subscript [-1] extracts the last element in a list

1-
1-23 23
Outline

1. Accessing Characters and


Substrings

2. Strings and Number


Systems

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:

Generate 100 random integer from 1 to 100 and write to a file


named intergers.txt

1-
1-26 26
ReadingText from a File
Assume that your current working directory contain the file
myfile.txt

Alternatively, read and process one line at a time.

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:

Kenneth A. Lambert (2012). Fundamentals of


Python: First Programs 1st Cengage Learning.

1-33

You might also like