Introduction to the capwords () method in the Python string module

grammar

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

description

In case of default sep parameter

The capwords () method is used to split the incoming string into a sequence by split () according to spaces, capitalize each first letter of the sequence, and then use spaces to form a string to return.

When specifying the sep parameter

The split () function uses the parameters passed to sep to split and group and pass in the string.

Code example

# 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) // Python Is One Of The Best Programming Languages.

# sep parameter is 'o' 
formatted = string.capwords(sentence, sep = 'o') 
print('When sep = "o"', formatted) // When sep = "o" PythoN is oNe oF the best proGramming languages.

Published 89 original articles · praised 83 · visits 3492

Guess you like

Origin blog.csdn.net/devin_xin/article/details/105394311