Skip to content

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:

Input
s = "hello", n = 1
Output
'hEllo'
Input
s = "python", n = 3
Output
'pytHon'
Input
s = "python", n = 9
Output
'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:

s[:n]   +   s[n].upper()   +   s[n+1:]
before      target char        after

Tracing the examples

Example 1: s = "hello", n = 1

s[:1]        →  "h"
s[1].upper() →  "E"
s[2:]        →  "llo"
Result       →  "hEllo" 

Example 2: s = "python", n = 3

s[:3]        →  "pyt"
s[3].upper() →  "H"
s[4:]        →  "on"
Result       →  "pytHon" 

Example 3: s = "python", n = 9

len("python") = 6
9 >= 6  →  out of bounds
Return "python" unchanged 

Solution approaches

def upper_nth_index_char(s: str, n: int) -> str:
    if n >= len(s):
        return s                          # out of bounds → unchanged
    before = s[:n]                        # everything before index n
    capitalised = s[n].upper()            # the target character
    after = s[n + 1:]                     # everything after index n
    return before + capitalised + after
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

01

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.

02

Bounds check first

Always handle the out-of-bounds case before accessing s[n]. Use n >= len(s) as the guard.

03

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.