Lesson 2 of 10
Programming: Lesson 2

Input, Output and String Handling

Programs are useless unless they can communicate with the world. This lesson covers reading input, producing output, and the string operations that appear on almost every GCSE exam paper.

55 minutes Python + C# toggle
Language: Saved automatically

Every useful program talks to the outside world. A program that cannot receive input is just a calculator running in a sealed box. A program that cannot produce output is thinking thoughts nobody can hear. And since almost all input arrives as text - typed by a user, read from a file, fetched from the internet - understanding how to manipulate strings is not optional. It is the skill you use in every single program you will ever write.

Think about it: A student writes a program that asks for a full name and outputs the initials. Before looking at any code, write down in English every step you would need to take. How do you split a string? How do you get the first letter? How do you join letters back together? That sequence of steps is exactly what string methods are designed to do.
Terms you need to know
Concatenation
Joining two strings end-to-end using +. "Hello" + " " + "World" gives "Hello World".
String index
The position of a character. Counting starts at 0 in both Python and C#. "hello"[0] = 'h'.
Slicing
Extracting a substring using start and end positions. Python: s[1:4]. C#: s.Substring(1, 3).
String length
The number of characters in a string. Python: len(s). C#: s.Length.
ASCII
American Standard Code for Information Interchange. Maps each character to a number: 'A'=65, 'a'=97, '0'=48.
ord() / char
Convert a character to its ASCII number (ord in Python, (int)char in C#) and back (chr() in Python, (char)int in C#).
Getting data in and out

Every interactive program needs to accept data from the user and display results. In Python, input() always returns a string regardless of what the user types. In C#, Console.ReadLine() does the same. You must cast the result if you need a number.

# Python input and output
name = input("Enter your name: ")  # always a string
age = int(input("Enter your age: "))   # cast to int

print("Hello,", name)           # comma inserts a space
print("You are" + str(age) + " years old")  # concatenation
print(f"Hello, {name}! You are {age} years old.")  # f-string (preferred)
// C# input and output
Console.Write("Enter your name: ");
string name = Console.ReadLine();    // always a string
Console.Write("Enter your age: ");
int age = int.Parse(Console.ReadLine());  // cast to int

Console.WriteLine("Hello, " + name);
Console.WriteLine("You are " + age + " years old");
Console.WriteLine($"Hello, {name}! You are {age} years old."); // interpolation
print() vs Console.Write() vs Console.WriteLine()

Python's print() always adds a newline at the end. In C#, Console.Write() prints without a newline, while Console.WriteLine() adds one. Use Console.Write() when you want input on the same line as a prompt.

Manipulating text

Strings support a rich set of operations. The table below shows the most important ones for GCSE - all appear regularly in exam questions.

OperationPythonC#Example output
Lengthlen(s)s.Length"hello" - 5
Uppercases.upper()s.ToUpper()"hello" - "HELLO"
Lowercases.lower()s.ToLower()"HELLO" - "hello"
Character at indexs[i]s[i]"hello"[1] - 'e'
Slice / Substrings[1:4]s.Substring(1, 3)"hello"[1:4] - "ell"
Find positions.find("l")s.IndexOf("l")"hello".find("l") - 2
Replaces.replace("l","r")s.Replace("l","r")"hello" - "herro"
Split by delimiters.split(",")s.Split(',')"a,b,c" - ["a","b","c"]
Char to ASCIIord('A')(int)'A'65
ASCII to charchr(65)(char)65'A'

Slicing in depth - Python vs C#:

s = "CodeBash"
#   C o d e B a s h
#   0 1 2 3 4 5 6 7  (index)

print(s[0])      # 'C'       - character at index 0
print(s[4:8])    # 'Bash'    - index 4 up to (not including) 8
print(s[:4])     # 'Code'    - from start up to 4
print(s[4:])     # 'Bash'    - from 4 to end
print(s[-1])     # 'h'       - last character
print(s[::-1])   # 'hsaBedoC' - reversed
string s = "CodeBash";
//   C o d e B a s h
//   0 1 2 3 4 5 6 7  (index)

Console.WriteLine(s[0]);              // 'C'
Console.WriteLine(s.Substring(4, 4)); // 'Bash'  - start at 4, take 4 chars
Console.WriteLine(s.Substring(0, 4)); // 'Code'
Console.WriteLine(s.Substring(4));    // 'Bash'  - from 4 to end
// C# Substring(start, length) - note: second arg is LENGTH not end index
// s[^1] gives last character in C# 8+
Exam angle

The most common string exam questions: (1) Find the substring "ell" in "hello" - give the index and length. (2) A student uses s[1:4] and expects "hell" but gets "ell" - explain the off-by-one. (3) Write code to check if a string starts with a capital letter. (4) ASCII: what is ord('B')? What character is chr(99)? Know that A=65, a=97, 0=48 - the rest follow consecutively.

ASCII and character codes:

# Converting between characters and ASCII codes
print(ord('A'))    # 65
print(ord('a'))    # 97  (lowercase is 32 more than uppercase)
print(ord('0'))    # 48
print(chr(66))     # 'B' (one more than 'A')

# Practical use: shift cipher (Caesar cipher)
char = 'A'
shifted = chr(ord(char) + 3)  # 'D'
// C# character to ASCII and back
Console.WriteLine((int)'A');    // 65
Console.WriteLine((int)'a');    // 97
Console.WriteLine((int)'0');    // 48
Console.WriteLine((char)66);    // 'B'

// Shift cipher example
char c = 'A';
char shifted = (char)((int)c + 3);  // 'D'
String Lab
String Operation Explorer
Type any string and click an operation to see the Python and C# code with the live result.
Click an operation above...

Three Quick Challenges

Predict the output

What does this print? Remember: slicing is [start:end] and the end index is excluded.

s = "CodeBash"
print(s[4:8])
string s = "CodeBash";
Console.WriteLine(s.Substring(4, 4));
Fill in the blank

Join two strings together with the concatenation operator:

greeting = "Hello" " World"
string greeting = "Hello" " World";
Spot the bug

This code crashes. What is the error?

age = input("Enter age: ")
total = age + 5
print(total)

Test yourself

1. What does "hello"[1:4] evaluate to in Python?

Python slicing s[start:end] includes the character at start but excludes the character at end. s[1:4] takes characters at positions 1, 2 and 3 - which are 'e', 'l', 'l' giving "ell". The 'h' at position 0 and 'o' at position 4 are not included.

2. In C#, what does "hello".Substring(2, 3) return?

C# Substring(startIndex, length) - start at index 2 ('l'), take 3 characters: 'l', 'l', 'o' giving "llo". This is different from Python's slice where the second argument is the END index, not the LENGTH. This distinction is a common exam trap.

3. What is the value of ord('D') in Python?

A=65, B=66, C=67, D=68. ASCII values for uppercase letters start at 65 and increase by 1 for each letter. Lowercase letters start at 97 (a=97, b=98, etc.). Remember: the difference between upper and lower case is always 32.

4. A program has: name = "Alice Smith". Which line extracts just "Alice"?

"Alice" occupies positions 0,1,2,3,4 ('A','l','i','c','e'). name[0:5] takes indices 0 to 4 inclusive (stops before 5). name[0:6] would give "Alice " including the space. name[:6] also gives "Alice " (6 chars). name[1:5] gives "lice".

5. What does len("CodeBash" + "!") return?

Concatenation happens first: "CodeBash" + "!" = "CodeBash!" which has 9 characters. len() then returns 9. The + operator on two strings always concatenates them (never adds).
Challenge question

Write a program that reads a full name (e.g. "Alice Smith") and outputs the initials (e.g. "A.S."). Your solution must work for any two-part name - not just "Alice Smith". Then extend it: how would you handle a three-part name like "Mary Ann Jones"? Describe the extra logic needed.

Two-part name solution (Python):
name = input("Full name: ")
parts = name.split(" ")
initials = parts[0][0] + "." + parts[1][0] + "."
print(initials)

Explanation: split(" ") divides the string at each space, returning a list. parts[0][0] gets the first character of the first word, parts[1][0] gets the first character of the second word.

Three-part name extension: Loop through all parts of the split list: initials = ".".join(p[0] for p in parts) + "." - or use a FOR loop over parts, appending each p[0] + "." to a running string. You must also handle edge cases: extra spaces between names, or names with only one part.
Printable Worksheets

Practice what you have learned

Three levelled worksheets. Download, print and complete offline.

Recall
String Operations Reference
Match operations to outputs, fill in method names and trace string manipulations step by step.
Download
Apply
String Processing Programs
Write programs to extract substrings, convert cases, count characters and use ASCII codes.
Download
Exam Style
Exam-Style Questions
Predict outputs, identify errors in string code, write Caesar cipher routines and trace slice operations.
Download
Lesson 2 - Programming
Input, Output and String Handling
Starter activity
Write "hello" on the board with index numbers underneath (0-4). Ask: what is at position 1? What about position 3? Then ask: what does [1:3] give you? Let students answer before revealing. This establishes zero-indexing and slicing intuitively before any code is shown.
Lesson objectives
1
Use input() and Console.ReadLine() to read user input and cast to required types.
2
Use print() / Console.WriteLine() to output values, including f-strings and string interpolation.
3
Apply string operations: length, upper, lower, index access, slicing and find.
4
Explain the difference between Python slicing (end index) and C# Substring (length).
5
Convert between characters and ASCII codes using ord/chr in Python and (int)/(char) in C#.
Key vocabulary
Concatenation
Joining strings with +. "Hello" + " " + "World" = "Hello World".
Zero-indexed
First character is at position 0, not 1. Common source of off-by-one errors.
ASCII
Maps 128 characters to integers. A=65, a=97, 0=48. Lowercase = uppercase + 32.
Discussion questions
Why does indexing start at 0 rather than 1? What would break if it started at 1?
Python's slice s[1:4] and C#'s Substring(1,3) both extract "ell" from "hello". Which API design do you find more intuitive? Why?
Why is ASCII still important even though modern computers use Unicode? Where would you use ord() in a real program?
Exit tickets
What does "Python"[2:5] evaluate to? Explain your reasoning. [2 marks]
Write code to check if the first character of a string is uppercase. [2 marks]
What is the output of chr(ord('m') - 32) and why? [2 marks]
Homework suggestion
Write a simple Caesar cipher program: ask the user for a message and a shift value (integer). Output the encrypted message by shifting each letter's ASCII code by the shift amount. Handle the wrap-around from Z back to A. Test with shift=3: "Hello" should become "Khoor".