Back to 课程

Computer Science GCES OCR

0% Complete
0/0 Steps
  1. Cpu Architecture Performance And Embedded Systems Ocr
    5 主题
  2. Primary And Secondary Storage Ocr
    6 主题
  3. Data Storage And Compression Ocr
    12 主题
  4. Networks And Topologies Ocr
    6 主题
  5. Wired And Wireless Networks Protocols And Layers Ocr
    6 主题
  6. Identifying And Preventing Threats To Computer Systems And Networks Ocr
    2 主题
  7. Operating Systems And Utility Software Ocr
    2 主题
  8. Ethical Legal Cultural And Environmental Impact Ocr
    2 主题
  9. Computational Thinking Searching And Sorting Algorithms Ocr
    3 主题
  10. Designing Creating And Refining Algorithms Ocr
    5 主题
  11. Programming Fundamentals And Data Types Ocr
    5 主题
  12. Additional Programming Techniques Ocr
    7 主题
  13. Defensive Design And Testing Ocr
    6 主题
  14. Boolean Logic Diagrams Ocr
    2 主题
  15. Programming Languages And Integrated Development Environments Ides Ocr
    3 主题
  16. The Exam Papers Ocr
    2 主题
  17. Structuring Your Responses Ocr
    3 主题
课 Progress
0% Complete

Exam code:J277

String Manipulation

What is string manipulation?

  • String manipulation is the use of programming techniques to modify, analyse or extract information from a string

  • Examples of string manipulation include:

    • Case conversion (modify)

    • Length (analyse)

    • Substrings (extract)

    • Concatenation (modify)

    • ASCII conversion (analyse)

Case conversion

  • The ability to change a string from one case to another, for example, lower case to upper case

Function

OCR exam reference

Python

Output

Uppercase

Name = "Sarah"

print(Name.upper)

Name = "Sarah"

print(Name.upper())

“SARAH”

Lowercase

Name = "SARAH"

print(Name.lower)

Name = "SARAH"

print(Name.lower())

“sarah”

Title case

Book = "inspector calls"

print(Book.title)

Book = "inspector calls"

print(Book.title())

“Inspector Calls”

Length

  • The ability to count the number of characters in a string, for example, checking a password meets the minimum requirement of 8 characters

Function

OCR exam reference

Python

Output

Length

Password = "letmein"

print(Password.length)

Password = "letmein"

print(len(Password))

7

Password = "letmein"

if Password.length >= 8 then

print("Password accepted")

else

print("Password too short")

end if

Password = "letmein"

if len(Password) >= 8:

print("Password accepted")

else:

print("Password too short")

“Password too short”

Substring

  • The ability to extract a sequence of characters from a larger string in order to be used by another function in the program, for example, data validation or combining it with other strings

  • Extracting substrings is performed using ‘slicing’, using specific start and end to slice out the desired characters

  • Substring is 0 indexed (first value is 0 not 1)

Function

OCR exam reference

Python

Output

 Substring

.substring(starting character, number of characters)

string[start character : end character]

 

Word = "Revision"

print(Word.substring(2,3))

Word = "Revision"

print(Word[2:5])

“vis”

.left(number of characters)

 

 

Word = "Revision"

print(Word.left(4))

Word = "Revision"

print(Word[:4])

“Revi”

.right(number of characters)

 

 

Word = "Revision"

print(Word.right(4))

Word = "Revision"

print(Word[4:])

“sion”

Concatenation

  • The ability to join two or more strings together to form a single string

  • Concatenation uses the ‘+‘ operator to join strings together

Function

OCR exam reference

Python

Output

Concatenation

FName = "Sarah"

SName = "Jones"

FullName = FName + SName

print(FullName)

FName = "Sarah"

SName = "Jones"

FullName = FName + SName

print(FullName)

“SarahJones”

FName = "Sarah"

SName = "Jones"

FullName = FName + " " + SName

print(FullName)

FName = "Sarah"

SName = "Jones"

FullName = FName + " " + SName

print(FullName)

“Sarah Jones”

Name = "Sarah"

print("Hello, " + Name)

Name = "Sarah"

print("Hello, " + Name)

“Hello, Sarah”

ASCII conversion

  • The ability to return an ASCII character from a numerical value and vice versa

Function

OCR exam reference

Python

Output

ASCII conversion

print(ASC(A))

print(ord("A"))

“65”

print(CHR(97))

print(chr(97))

“a”

Examiner Tips and Tricks

Remember that the ‘+’ operator is used for concatenation of strings BUT is also the mathematical operator for addition

It is important to remember, that the same operator symbol performs different roles on different types of data (integer/string)

Worked Example

A school wants to use a program to take a students first name, last name and year of entry as inputs and use them to create a username

They want the username to follow the rule:

  • Initial + First 3 letters of last name + year

For example, a student named David Hamilton who started in 2024 would have the username:

  • DHam2024

The algorithm has been started below:

Line

Algorithm

01

FName = input("Enter first name")

02

LName = input("Enter last name")

03

year = input("Enter year")

04

username = 

05

print(username)

Use string manipulation to complete line 04 to create the username [3]

How to answer this question

  • What techniques do we need to use to create the username? substring to extract the parts of the first and last name

  • Concatenation to join them together

Answer

Line

Algorithm

01

FName = input("Enter first name")

02

LName = input("Enter last name")

03

year = input("Enter year")

04

username = FName.substring(0,1) + LName.substring(0,3) + year

05

print(username)

Guidance

  • FName.substring(0,1) 1 mark

  • LName.substring(0,3) 1 mark

  • username = FName.substring(0,1) + LName.substring(0,3) + year 1 mark

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注