💡
Exercise 21

String Basics 10 XP Easy

Ctrl+Enter Run Ctrl+S Save

Welcome to Strings — one of the most common data types in coding interviews! Almost every company asks string questions. 📝

💡 Think of it like this: A string is just an array of characters. The string 'hello' is really ['h', 'e', 'l', 'l', 'o']. So all array techniques work on strings too!

s = 'hello' print(s[0]) # 'h' (access by index) print(s[-1]) # 'o' (last character) print(len(s)) # 5 (length) print(s[::-1]) # 'olleh' (reverse!) print(s.upper()) # 'HELLO' print(s.lower()) # 'hello' print('ell' in s) # True (substring check)

Key string facts for interviews:

  • ⚠️ Strings in Python are immutable — you can't change a character in-place
  • Converting to list and back: list(s) → modify → ''.join(list)
  • String comparison is O(n) — comparing character by character
  • Common techniques: two pointers, hash maps, sliding window

Essential string methods:

  • s.isalpha() — Is it all letters?
  • s.isdigit() — Is it all numbers?
  • s.isalnum() — Letters or numbers?
  • s.split() — Split into words
  • ' '.join(words) — Join words with space
  • s.strip() — Remove whitespace from edges
📋 Instructions
Write a function `string_info(s)` that takes a string and prints: - The length - The first and last character - The string reversed - The string in uppercase - Number of vowels (a, e, i, o, u — case insensitive)
Use len(s) for length, s[0] and s[-1] for first/last, s[::-1] for reverse, s.upper() for uppercase. For vowels: sum(1 for c in s.lower() if c in 'aeiou').
🧪 Test Cases
Input
Run your code
Expected
Length: 7 First: C Last: x ...
Expected output (6 lines)
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.