Skip to content

Python capitalize every other letter | Example code

  • by

Using for loop you can capitalize every other letter in Python programming. Iterate over the given string and capitalize car every other char with the if condition. Add both types of char into the new string.

Example capitalise every other letter in a string in Python

Simple python example code has a function to capitalize every other letter.

def mock(s):
    res = ""
    i = True
    for char in s:
        if i:
            res += char.upper()
        else:
            res += char.lower()
        i = not i
    return res


print(mock("Hello word program"))

Output:

Python capitalize every other letter

Do comment if you have any other code or doubts on this Python capitalize topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading