2.2 String manipulation + BASIC FILE HANDLING Flashcards
Define string manipulation
String Manipulation: “The act of manipulating, extracting or changing the characters in a string variable”
State the string manipulation commands
string manipulation commands
.length
.upper
.lower
.substring (x, i)
ASC(…)
CHR(…)
What does .length mean
.length is used to get the length of a string
Returns the length of a string
length = string.length - if the string was “Hello” then length would be 5
What does .upper and .lower mean
.upper and .lower
Used for converting the cases of a string
Returns the string in uppercase and lowercase
What deos .substring(x, i) mean
what will output
chars=”Hello”
chars = string.substring (3,1)
print(chars)
substring(x, i)
to get a substring
Returns part of a string, starting at the character of the first parameter and up to (not including) the number of characters in the second parameter
stringname.subString(startingPosition, numberofCharacters)
chars = string.substring (3,1) - index starts at 1
If string was “Hello” then chars would be “I” (lowercase L)
what does ASC(…) mean
ascii=ASC(“A”)
print(ascii)
what would output
Ascii conversion
ASC(…)
Returns the ASCII value of a character
ASC(character)
e.g.
ascii=ASC(“A”)
ascii would be 65
What does CHR(…) mean
Char = CHR(65)
print(Char)
what would output
Ascii conversion
CHR(…)
Returns the character from the ASCII number
CHR (asciinumber)
Char = CHR(65)
char would be “A”
What will this code display
someText=”Computer Science”
print(someText.length
print(someText.substring(3,3)).upper)
Will display:
16 - includes spaces
PUT
State the string manipulation commands in python
string manipulation commands in python
.len
.upper()
.lower()
.substring (x, i)
ord(…)
chr(…)
what does .len mean
write a program to work out the length of this list
“Hello”
what would be the output
In a high level programming language
Returns the length of a string
string=”Hello”
length = len (string)
print(length)
- if the string was “Hello” then length would be 5
what does .upper and .lower mean
write a program which displays the word “Hello” in uppercase and lowercase
In a high level programming language
Returns the string in uppercase or lowercase
ustring = string.upper() -
if the string was “Hello” then ustring would be “HELLO”
lstring = string.lower() -
If the string was “Hello” then lstring would be “hello”
what does .substring (x, i) do
write a program which splits “Hello” into a sublist to become “l” - 1st L
In a high level programming language
- substring - Returns part of a string, starting at the character of the first parameter and the ending position in the second parameter
stringname.subString(startingPosition, endingPosition)
string=”Hello”
chars = string[2:3] - index starts at 0
what does ord(…) do
ord (…)
Returns the ASCII value of a character
Write a program which returns the ASCII value of the character “A” - in a high level programming language
what would the output be
ascii = ord(“A”)
print(ascii)
ord(character)
outuput - 65
What does chr(…) do
chr (…) Returns the character from the ASCII number
write a program which returns the character from the ascii value “65” - IN A HIGH LEVEL PROGRAMMING LANGUAGE
what would the output be
char = chr(65)
print(char)
output
A
what will be the output of this program
someText=”Computer Science”
print(len(someText))
print((someText[3:7].upper())
Program will display
16
PUTE
How to write data to a file
Open the file for creating/overwriting or appending to a file
Write data to a file
Close the file
What are the stages of reading data from a file
Open the file for reading data
Assign a boolean variable to “false” to indicate the end of the file is not reached
While the end of the file is false and the search item is not found:
Read the file from data
If the data matches what is being searched for, assign the data to variable or output
Check if the end of the file has been reached and assign the Boolean variable to “True”
Close the file
In python, what do you write to open a file, with the intention to read from the file
f = open(“characters.txt, “r”)
f - is a variable
The open command takes two parameters:
“characters” - the name of the file to open
“r” - what we want to do with the file e.g. read
In python how do you indicate that you have not reached the end of the file
end_of_file = False
or
while not end_of_file:
- end_of_file is a variable
In python, what do you write to read a file
f = open(“characters.txt, “r”)
….
name=f.readline().strip()
health=f.readline().strip()
name and health are variables
f - was assigned earlier as being a pointer to characters.txt
.strip() - this strips the new line character - so we just get the name/ the thing we want. the new line character is there, as when writing the file - we were pressing enter - the enter is stored as a character - to signify a new line
In python how do you close the file
f.close()
f is just a variable
f is just pointing to characters.txt
f is the thing connecting our program to our text file
In python how do you open a file, with the intention to write
f = open(“characters.txt”, “a”)
The open command takes two parameters
“characters.txt” the name of the file we want to connect to
“a” - means we want to append (add) to the end of the file
“w” - this would create a new file or overwrite contents of an existing file