Python string capwords() method Last Updated : 02 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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) OutputLearn 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.Table of ContentSyntax Of capwords() MethodExamples of String capwords()Frequently Asked Questions (FAQs) on capswords() MethodSyntax of capwords() Methodstring.capwords(string, sep=None)Parametersstring: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 TypeReturns 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 sentenceWhen 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) OutputLearn 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 separatorWhen 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) OutputLearn-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 stringsThis 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) OutputLearn 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. Comment More infoAdvertise with us Next Article Python String istitle() Method S sanjeev2552 Follow Improve Article Tags : Python python-string Python-string-functions Practice Tags : python Similar Reads Python String Methods Python string methods is a collection of in-built Python functions that operates on strings.Note: Every string method in Python does not change the original string instead returns a new string with the changed attributes. Python string is a sequence of Unicode characters that is enclosed in quotatio 5 min read Python String lstrip() Method The lstrip() method removes leading whitespace characters from a string. We can also specify custom characters to remove from the beginning/starting of the string.Let's take an example to remove whitespace from the starting of a string.Pythons = " Hello Python!" res = s.lstrip() print(res)OutputHell 2 min read String lower() Method in Python lower() method in Python converts all uppercase letters in a string to their lowercase. This method does not alter non-letter characters (e.g., numbers, punctuation). Let's look at an example of lower() method:Pythons = "HELLO, WORLD!" # Change all uppercase letters to lowercase res = s.lower() prin 3 min read Python String endswith() Method The endswith() method is a tool in Python for checking if a string ends with a particular substring. It can handle simple checks, multiple possible endings and specific ranges within the string. This method helps us make our code cleaner and more efficient, whether we're checking for file extensions 2 min read Python String istitle() Method The istitle() method in Python is used to check whether a string follows the title case formatting. In a title-cased string, the first letter of each word is capitalized, and all other letters in the word are in lowercase. This method is especially useful when working with formatted text such as tit 3 min read String capitalize() Method in Python The capitalize() method in Python is used to change the first letter of a string to uppercase and make all other letters lowercase. It is especially useful when we want to ensure that text follows title-like capitalization, where only the first letter is capitalized.Let's start with a simple example 2 min read Like