read text files

icon picker
bookingDate

demo: read text file, use substring
This is a demonstration program, it will show:
How to read a list of values from a text file
How to use substring to select some letters from a text value

constructs used

the program will use the following constructs
file handling
open file
read file
close file

data structure
• string array (to store the values from the file)
control structure
fixed loop using index (to repeat actions for each value in the file / array)
if conditional
substring function
startswith function

substring basics

substring is used to make a new string value by using a defined section of the original string
substring is a string function
the function uses two parameters to define the start and end of the section
in this example the first 5 characters of a message are used to display a single word
H is at position 0 in the string, o is at position 4
the 1st parameter for substring is the position of the 1st character
the 2nd parameter for substring is the (position of the last character) + 1

in this example the last 6 characters of a message are used
you can select a substring anywhere in the text: beginning, middle or end
notice that the ! is in position 11 and the length of the string is 12 characters.

in this example the first character of each variable are used to store initials
choosing a single character needs consecutive positions, in this case 0 and 1

Input requirements


Sample data from file

output requirements


Display full booking list
Bookings on the first day of the month
Bookings in April

design: main steps

code: repl project

Loading…


Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.