S1Q2 · Capitalize nth Character¶
⚡ Quick Reference
Function: upper_nth_index_char(s: str, n: int) -> str
Core idea: if n is out of bounds return s unchanged, otherwise rebuild the string with s[n] uppercased.
def upper_nth_index_char(s: str, n: int) -> str:
if n >= len(s):
return s
return s[:n] + s[n].upper() + s[n+1:]
Key rules:
- n is 0-indexed (example: n=1 capitalises the second character)
- If n >= len(s), return s unchanged
- Strings are immutable - you must rebuild with slicing, not direct assignment
Problem Statement¶
Problem
Write a function upper_nth_index_char(s: str, n: int) -> str that capitalises the character at index n in the string s. If n is greater than or equal to the length of s, return the string unchanged.
Examples:
s = "hello", n = 1
'hEllo'
s = "python", n = 3
'pytHon'
s = "python", n = 9
'python'
Understanding the problem¶
Two things to handle:
| Case | Condition | What to do |
|---|---|---|
n is valid |
n < len(s) |
Rebuild string with s[n] uppercased |
n is out of bounds |
n >= len(s) |
Return s unchanged |
Strings are immutable
You cannot do s[n] = s[n].upper() - Python strings do not support item assignment. The only way to "change" a character is to reconstruct the string using slicing:
Tracing the examples¶
Example 1: s = "hello", n = 1
Example 2: s = "python", n = 3
Example 3: s = "python", n = 9
Solution approaches¶
def upper_nth_index_char(s: str, n: int) -> str:
if n >= len(s):
return s
return s[:n] + s[n].upper() + s[n+1:]
Collapse the three parts into a single expression. This is the preferred form - clean and still easy to read.
def upper_nth_index_char(s: str, n: int) -> str:
if n >= len(s):
return s
chars = list(s) # convert to a mutable list of characters
chars[n] = chars[n].upper()
return "".join(chars)
Convert the string to a list (which is mutable), update index n, then rejoin. More steps than slicing, but makes the mutation logic explicit - useful when you need to change multiple characters.
def upper_nth_index_char(s: str, n: int) -> str:
if n >= len(s):
return s
return "".join(
c.upper() if i == n else c
for i, c in enumerate(s)
)
Walk through every character with its index; uppercase only when i == n. Compact but slightly over-engineered for this problem - good to know for cases where multiple positions need changing.
Common mistakes¶
Off-by-one
The index n is 0-based. n=1 means the second character (s[1]), not the first. Confirm this with the example: upper_nth_index_char("hello", 1) → 'hEllo' - 'h' is untouched, 'E' is at index 1.
Wrong boundary check
Use n >= len(s), not n > len(s). If s = "hi" (length 2) and n = 2, index 2 doesn't exist - so n >= len(s) correctly catches it.
Key takeaways¶
String immutability
You can't change a character in place. Always reconstruct: s[:n] + new_char + s[n+1:]. This pattern appears in many string problems.
Bounds check first
Always handle the out-of-bounds case before accessing s[n]. Use n >= len(s) as the guard.
str.upper() vs str.capitalize()
"hello".upper() → "HELLO" (all chars). s[n].upper() uppercases just one character. str.capitalize() uppercases only the first character of the whole string - not useful here.