• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Capitalize Every Other Letter of String

Using Python to Capitalize Every Other Letter of String

June 24, 2022 Leave a Comment

To capitalize every other letter of a string in Python, the easiest way is with a loop inside a function.

def capitalize_every_other(string):
    result = ""
    prev_char_cap = False #we want first letter to be capitalized
    for char in string:
        if prev_char_cap: 
            result = result + char.lower()
        else:
            result = result + char.upper()
        prev_char_cap = not prev_char_cap
    return result

print(capitalize_every_other("programming"))

#Output:
PrOgRaMmInG

If you have a string with spaces, and want to take spaces into account, then you can do the following.

def capitalize_every_other(string):
    result = ""
    prev_char_cap = False #we want first letter to be capitalized
    for char in string:
        if prev_char_cap: 
            result = result + char.lower()
        else:
            result = result + char.upper()
        if char != " ":
            prev_char_cap = not prev_char_cap
    return result

print(capitalize_every_other("programming is fun"))

#Output:
PrOgRaMmInG iS fUn

When working with strings in Python, the ability to easily manipulate and change the value of a string variable can be useful.

One such situation is if you want to capitalize every other letter of a string.

You can easily capitalize every other letter of a string in Python using a loop and the upper() and lower() functions.

First, we need to create an empty string and then also decide if we want the first letter to be capitalized or not.

Then, you can loop over each character in the string and if the previous character is capitalized, then we make the character lowercase. If the previous character is lowercase, then we make it uppercase.

Below is a function in Python which will capitalize every other character in a string.

def capitalize_every_other(string):
    result = ""
    prev_char_cap = False #we want first letter to be capitalized
    for char in string:
        if prev_char_cap: 
            result = result + char.lower()
        else:
            result = result + char.upper()
        prev_char_cap = not prev_char_cap
    return result

print(capitalize_every_other("programming"))

#Output:
PrOgRaMmInG

If you have a string with spaces, and want to take spaces into account, then you need one additional step. If the character is a space, then we shouldn’t update the previous character variable.

def capitalize_every_other(string):
    result = ""
    prev_char_cap = False #we want first letter to be capitalized
    for char in string:
        if prev_char_cap: 
            result = result + char.lower()
        else:
            result = result + char.upper()
        if char != " ":
            prev_char_cap = not prev_char_cap
    return result

print(capitalize_every_other("programming is fun"))

#Output:
PrOgRaMmInG iS fUn

Hopefully this article has been useful for you to learn how to capitalize every other letter in a string using Python.

Other Articles You'll Also Like:

  • 1.  Python acosh – Find Hyperbolic Arccosine of Number Using math.acosh()
  • 2.  for char in string – How to Loop Over Characters of String in Python
  • 3.  Multiple Condition While Loops in Python
  • 4.  Using Python to Search File for String
  • 5.  Convert String to Boolean Value in Python
  • 6.  Create List of Odd Numbers in Range with Python
  • 7.  Python turtle dot() – Draw Dot on Turtle Screen
  • 8.  Convert String to Integer with int() in Python
  • 9.  Python Get First Word in String
  • 10.  Get Day Name from Datetime in pandas DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x