Additional programming techniques

Basic String Manipulation

String manipulation is a fundamental aspect of programming, allowing you to modify and work with text data effectively. Here are some common techniques:

Concatenation

Joining two or more strings together using the + operator. You will often use this when providing suitable output. Remember that you can only concatenate strings, so you might need to cast a number into a string before you attempt to use the + operator.

Definition: Concatenation

Joining two or more strings together using a +

"Hello"+" "+"World"

Examples

stringA = "Hello"
stringB = "World"
print(stringA + stringB)  # Output: HelloWorld
print(stringA +" "+ stringB)  # Output: Hello World
print("Hello, your name is: " + name)  # Output: Hello, your name is: John

String Length

Getting the number of characters in a string.

Example:

subject = "ComputerScience"
print(len(subject))  # Output: 15

Substrings / Slicing

Definition: Substrings / Slicing

Extracting a portion, or slice, of a string.

Examples:

subject = "ComputerScience"
print(subject[3:9])  # Output: puterS (substring from index 3 to 9)
print(subject[:4])  # Output: Comp (first 4 characters)
print(subject[-3:])  # Output: nce (last 3 characters)

Case Conversion

  • Converting the whole string into uppercase or lower case

Examples

subject = "ComputerScience"
print(subject.upper())  # Output: COMPUTERSCIENCE
Lowercase: Convert all characters to lowercase.
subject = "ComputerScience"
print(subject.lower())  # Output: computerscience

ASCII Conversion

Definition: Converting characters to their ASCII values and vice versa.

Examples:

print(ord('A'))  # Output: 65 (ASCII value of 'A')
print(chr(97))  # Output: a (character for ASCII value 97)

String Tricks

Capitalise the first letter

  • Extract First Letter: original_string[0] extracts the first character.
  • Capitalize: .upper() converts it to uppercase.
  • Concatenate: Combine the capitalized first letter with the rest of the string using +.
# Original string
original_string = "computerScience"

# Extract the first letter and capitalize it
first_letter = original_string[0].upper()

# Concatenate the capitalized first letter with the rest of the string
new_string = first_letter + original_string[1:]

print(new_string)  # Output: ComputerScience

Exam Reference Language

Note Exam Reference Language

The exam reference language differs from python, you can find this in the back of the specification. You are allowed to write in python in the exam. You will need to understand how this works though in case it is given to you in a question.

String Length


subject = "ComputerScience"
print(subject.length) # Output: 15

Substrings

# Output: puterS (substring index 3 and 6 chars)
print(subject.substring(3,6)) 

print(subject.left(4)) # Output: Comp (first 4 characters)
print(subject.right(3)) # Output: nce (last 3 characters)

Case

print(subject.upper) # Output: COMPUTERSCIENCE
print(subject.lower) # Output: computerscience

ASCII

ASC(ꞌAꞌ) # returns 65 (numerical)
CHR(97) # returns ꞌaꞌ (char)
Previous
Maintainability