HCI 6. Strings and Text Files (2022) revised
Uploaded by adrianwang2003 · 28 May 2024
Preview
Hwa Chong Institution H2 Computing 1 6 Strings and Text Files Learning Outcome 6.1 Accessing Characters and Substrings in Strings 6.1.1 The Structure of Strings ● Data structure: Consists of smaller pieces of data ● String’s length: Number of characters it contains (0+) 6.1.2 The Subscript Operator The form of the subscript operator is: Subscript operator is useful when you want to use the positions as well as the characters in a string. For example, we can use a count-controlled loop below. Programming Elements and Consturcts Understand the data type string and use common library functions for strings Implementing Algorithms and Data Structures Store data in and retrieve data from serial and sequential text files
Hwa Chong Institution H2 Computing 2 6.1.3 Slicing for Substrings Python’s subscript operator can be used to obtain a substring through a process called slicing. Place a colon (:) in the subscript; an integer value can appear on either side of the colon. 6.1.4 Testing for a Substring with the in Operator When used with strings, the left operand of in is a target substring and the right operand is the string to be searched. Returns True if target string is somewhere in search string, or False otherwise.
Hwa Chong Institution H2 Computing 3 6.2 String Methods Python includes a set of string operations called methods that make tasks like counting the words in a single sentence easy. A method behaves like a function, but has a slightly different syntax. A method is always called with a given data value called an object. ● Methods can expect arguments and return values ● A method knows about the internal state of the object with which it is called ● In Python, all data values are in fact objects
Hwa Chong Institution H2 Computing 4
Hwa Chong Institution H2 Computing 5 Example: counting number of words Example: extracting a filename’s extension The subscript [ -1] extracts the last element. It can be used to write a general expression for obtaining any filename’s extension, as follows: A string is an immutable data structure . When we use any method, it does not change the string itself. Note the difference of the following two sets of code.
Hwa Chong Institution H2 Computing 6 6.3 Text Files A text file is software object that stores data on permanent medium such as disk or CD. When compared to keyboard input from human user, the main advantages of taking input data from a file are: ● The data set can be much larger ● The data can be input much more quickly and with less chance of error ● The data can be used repeatedly with the same program or with different programs 6.3.1 Text Files and Their Format Using a text editor such as Notepad or TextEdit, you can create, view, and save data in a text file. All data output to or input from a text file must be strings.
Content continues in the PDF.
Related notes
- ACJC 2025 JC2 Computing Prelim Paper 1Exam Papers · 2025
- ACJC 2025 JC2 Computing Prelim Paper 1 SolutionsExam Papers · 2025
- VJC Chapter 23 Web Applications PrinciplesNotes/Practices · 2025
- VJC Chapter 20 SQLNotes/Practices · 2025
- VJC Chapter 16 Hash TableNotes/Practices · 2025
- VJC Chapter 22 NoSQLNotes/Practices · 2025

