Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

Hello guys, in the last article we learned about the capitalize function that converts the first character of a string. At the end of the article we also asked that capitalize can only capitalize the first character, so what if we wanted to capitalize the first letter of every word. That’s the other function we’re going to learn today, title

The following string will be used as an example string throughout this series: mystr = ‘Hello world and hello Python’

title(self)

The title function takes no arguments like capitalize, and the syntax and usage are exactly the same as capitalize, except that title will capitalize the first letter of every word.

  • Purpose: Title Converts the first letter of every word in a string to uppercase and returns the converted new string
  • Grammar; title(self)
  • Usage; xxx.title(self)

Where XXX represents a complete string, the self argument is not passed and no additional arguments are required.

Let’s look at a simple example of converting the first letter of each word in ‘Hello World and Hello Python’ to uppercase:

mystr = 'hello world and hello python'

print(mystr.title())
# output result
Hello World And Hello Python
Copy the code

conclusion

And now we’ve learned a new lesson about capitalize, which is a perfect complement to the capitalize that we learned in the last article, that you can capitalize just the first letter of the entire string and capitalize every single word. This is the sharing of the title built-in function. Like the small partner about the point like message plus attention oh