• 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 / How to Capitalize the First Letter of Every Word in Python

How to Capitalize the First Letter of Every Word in Python

February 16, 2022 Leave a Comment

In Python, we can capitalize the first letter of every word in a string easily with the help of slicing, Python split() function, and the Python upper() function.

string = "this is a string with some words"

def capitalizeFirstLetter(string):
    new_strings = []
    for x in string.split(" "):
        new_strings.append(x[0].upper() + x[1:])
    return " ".join(new_strings)

print(capitalizeFirstLetter(string))

#Output:
This Is A String With Some Words

When using string variables in Python, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is to capitalize the first letter of every word in a string.

We can easily capitalize the first letter of every word in Python.

First, we can use the split() function to split the string by spaces to get a list of the words of the string variable. Then, we can loop over each word, and use slicing to get the first character of the string to capitalize with upper() and concatenate that back to the rest of the word.

At the end, we can join the list of capitalized words back together with the join() function.

Below is an example Python function of how you can capitalize the first letter of each word in a string variable.

string = "this is a string with some words"

def capitalizeFirstLetter(string):
    new_strings = []
    for x in string.split(" "):
        new_strings.append(x[0].upper() + x[1:])
    return " ".join(new_strings)

print(capitalizeFirstLetter(string))

#Output:
This Is A String With Some Words

How to Make the First Letter of Each Word Lowercase in Python

If you want to go the other way and make the first letter of each word in a string variable lowercase, we can make a small adjustment to our function.

Instead of using the Python upper() function, we can use the lower() function.

Below is an example Python function of how you can make the first letter of each word lowercase.

string = "THIS IS A STRING OF SOME WORDS"

def lowercaseFirstLetter(string):
    new_strings = []
    for x in string.split(" "):
        new_strings.append(x[0].lower() + x[1:])
    return " ".join(new_strings)

print(lowercaseFirstLetter(string))

#Output:
tHIS iS a sTRING oF sOME wORDS

Hopefully this article has been useful for you to learn how to capitalize the first letter of each word in Python.

Other Articles You'll Also Like:

  • 1.  Using Selenium to Check if Element Exists in Python
  • 2.  Remove Specific Word from String in Python
  • 3.  How to Sort Numbers in Python Without Sort Function
  • 4.  pandas Drop Columns – Delete Columns from a DataFrame
  • 5.  Get Month Name from Date in Python
  • 6.  How to Iterate over Everything in Word Document using python-docx
  • 7.  Convert First Letter of String to Lowercase in Python
  • 8.  Deque Peek and Queue Peek Functions in Python
  • 9.  How to Slice a Dictionary in Python
  • 10.  Reverse String with Recursion in Python

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