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