The Wayback Machine - https://web.archive.org/web/20250328090751/https://www.geeksforgeeks.org/python-string-capwords-method/
Open In App

Python string capwords() method

Last Updated : 02 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

capwords() method in Python is a part of the string module and is used to capitalize the first letter of every word in a given string while converting all other letters to lowercase. To use capwords(), the string module must be imported as it is not a built-in string method.

Python
import string

s = "learn python with geeksforgeeks!"
formatted = string.capwords(s)
print(formatted) 

Output
Learn Python With Geeksforgeeks!

Explanation:

  • The string.capwords() method processes the string ‘s' by splitting it into words, capitalizing the first letter of each word and joining them back together.
  • The method also converts any uppercase letters in the middle of a word to lowercase.
  • Punctuation marks are not removed but remain part of the resulting string.

Syntax of capwords() Method

string.capwords(string, sep=None)

Parameters

  • string:
    • The input string to be processed.
  • sep (optional):
    • The delimiter used to separate words in the string. If omitted, any whitespace is treated as the separator.

Return Type

  • Returns a new string where the first letter of each word is capitalized, and all other letters are converted to lowercase.

Examples of String capwords()

1. Capitalizing words in a simple sentence

When working with sentences, ensuring consistent capitalization can significantly enhance readability. This method is especially useful in titles and headings.

Python
import string

s = "learn python with geeksforgeeks!"
formatted = string.capwords(s)
print(formatted)  

Output
Learn Python With Geeksforgeeks!

Explanation:

  • The input string ‘s' contains all lowercase words.
  • The capwords() method capitalizes the first letter of each word and lowers the rest, resulting in proper formatting.

2. Using a custom separator

When strings use specific separators like commas or hyphens instead of spaces, the sep parameter can be used for precise formatting.

Python
import string

s = "learn-python-with-geeksforgeeks!"
formatted = string.capwords(s, sep="-")
print(formatted)  

Output
Learn-Python-With-Geeksforgeeks!

Explanation:

  • The sep parameter specifies "-" as the delimiter.
  • The method capitalizes the first letter of each segment separated by the hyphen and keeps the separator intact.

3. Handling mixed-case strings

This method is ideal for normalizing text where some words are already capitalized or contain uppercase letters.

Python
import string

s = "lEARn pythON wITh gEEksfORgEEks!"
formatted = string.capwords(s)
print(formatted)  

Output
Learn Python With Geeksforgeeks!

Explanation:

  • The input string contains a mix of uppercase and lowercase letters.
  • The capwords() method converts all letters to lowercase first, then capitalizes the first letter of each word.

Frequently Asked Questions on capswords() Method

1. Can I use capwords() without importing the string module?

No, capwords() is not a built-in method and requires importing the string module.

import string
# Required to use capwords()

2. How does capwords() differ from the title() method?

The title() method capitalizes the first letter of every word but may not handle mixed-case words correctly. capwords(), on the other hand, ensures proper formatting by converting all other letters to lowercase.

3. Does capwords() alter punctuation or whitespace?

No, punctuation and separators remain part of the resulting string. Whitespace is treated as a default separator if no custom delimiter is specified.



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg