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.
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.
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
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.
Strings support a rich set of operations. The table below shows the most important ones for GCSE - all appear regularly in exam questions.
| Operation | Python | C# | Example output |
|---|---|---|---|
| Length | len(s) | s.Length | "hello" - 5 |
| Uppercase | s.upper() | s.ToUpper() | "hello" - "HELLO" |
| Lowercase | s.lower() | s.ToLower() | "HELLO" - "hello" |
| Character at index | s[i] | s[i] | "hello"[1] - 'e' |
| Slice / Substring | s[1:4] | s.Substring(1, 3) | "hello"[1:4] - "ell" |
| Find position | s.find("l") | s.IndexOf("l") | "hello".find("l") - 2 |
| Replace | s.replace("l","r") | s.Replace("l","r") | "hello" - "herro" |
| Split by delimiter | s.split(",") | s.Split(',') | "a,b,c" - ["a","b","c"] |
| Char to ASCII | ord('A') | (int)'A' | 65 |
| ASCII to char | chr(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+
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'
Three Quick Challenges
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));
Join two strings together with the concatenation operator:
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?
2. In C#, what does "hello".Substring(2, 3) return?
3. What is the value of ord('D') in Python?
4. A program has: name = "Alice Smith". Which line extracts just "Alice"?
5. What does len("CodeBash" + "!") return?
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.
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.
Practice what you have learned
Three levelled worksheets. Download, print and complete offline.