Python capitalize every other letter | Example code

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.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.