Recommended Free Tools
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.
#1 Best Overall
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().
Rank #2
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.
Quick Recap
Best Value
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.




