Thursday, October 2, 2025
HomeLanguagesPython string | capwords() method

Python string | capwords() method

In Python, string capwords() method is used to capitalize all the words in the string using split() method. 
 

Syntax: string.capwords(string, sep=None) 
Return Value: Returns a formatted string after above operations.

Split the argument into words using split, capitalize each word using capitalize, and join the capitalized words using join. If the optional second argument sep is absent or None, runs of whitespace characters are replaced by a single space and leading and trailing whitespace are removed, otherwise sep is used to split and join the words.
Code #1: If sep parameter is left None
 

Python3




# imports string module
import string
 
sentence = 'Python is one of the best programming languages.'
 
# sep parameter is left None
formatted = string.capwords(sentence, sep = None)
 
print(formatted)


Output: 

Python Is One Of The Best Programming Languages.

 

  
Code #2: When sep is not None. 
 

Python3




# imports string module
import string
 
sentence = 'Python is one of the best programming languages.'
 
# sep parameter is 'g'
formatted = string.capwords(sentence, sep = 'g')
print('When sep = "g"', formatted)
 
# sep parameter is 'o'
formatted = string.capwords(sentence, sep = 'o')
print('When sep = "o"', formatted)


Output: 

When sep = "g" Python is one of the best progRamming langUagEs.
When sep = "o" PythoN is oNe oF the best proGramming languages.

 

RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11927 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7079 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS