Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk1 min

How to Capitalize Alternate Letters in a String in Python

Learn two Python approaches to alternating case: one counts every string position, while the other advances only for alphabetic characters.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To capitalize alternate characters by position, uppercase characters at even indexes and lowercase characters at odd indexes. If you want spaces and punctuation to be ignored when counting, use a separate counter that advances only for letters.

Alternate case at every string position

This version treats index 0 as the first uppercase position. Spaces, punctuation, and digits occupy positions too, so they affect the parity even though casing leaves them visually unchanged.

def alternate_case(text):
    return ''.join(
        ch.upper() if index % 2 == 0 else ch.lower()
        for index, ch in enumerate(text)
    )

print(alternate_case("hello, world!"))  # HeLlO, wOrLd!

enumerate() supplies each character and its index; ''.join(...) combines the resulting one-character strings. The code normalizes casing according to position: letters at even indexes become uppercase and those at odd indexes become lowercase, regardless of their original case. An empty string produces an empty string.

Alternate letters while ignoring spaces and punctuation

If only alphabetic characters should determine the pattern, keep a letter counter and increment it only when the current character qualifies as a letter. Other characters are copied unchanged.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
def alternate_letters(text):
    result = []
    letter_index = 0

    for ch in text:
        if ch.isalpha():
            result.append(ch.upper() if letter_index % 2 == 0 else ch.lower())
            letter_index += 1
        else:
            result.append(ch)

    return ''.join(result)

Python’s str.isalpha() recognizes Unicode letters, not just English A–Z. If the intended rule is ASCII letters only, use an explicit test for the ranges 'A' <= ch <= 'Z' or 'a' <= ch <= 'z' instead. As with the position-based version, the empty input naturally returns an empty string.

What counts as a character in Python?

Python documents strings as “immutable sequences of Unicode code points.” Indexing a string gives a one-character string, but a Unicode code point is not always the same as a visible character (grapheme) to a reader. For text where user-perceived characters may consist of multiple code points, these simple loops do not provide universal grapheme-aware alternation. See the Python 3.14.8 string documentation for the behavior of string indexing, casing methods, isalpha(), and join().

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why str.capitalize() is not the right method

str.capitalize() changes the first character and lowercases the rest; it does not alternate case through the string. Python documents it as: “Return a copy of the string with its first character capitalized and the rest lowercased.” See the official str.capitalize() reference.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.