{"id":3333,"date":"2021-04-22T17:10:32","date_gmt":"2021-04-22T11:40:32","guid":{"rendered":"https:\/\/python-programs.com\/?p=3333"},"modified":"2021-11-22T18:44:55","modified_gmt":"2021-11-22T13:14:55","slug":"python-capitalize-the-first-letter-of-each-word-in-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-capitalize-the-first-letter-of-each-word-in-a-string\/","title":{"rendered":"Python: Capitalize the First Letter of Each Word in a String?"},"content":{"rendered":"
A sequence of characters is referred to as a string.<\/p>\n
Characters are not used by computers instead, numbers are used (binary). Characters appear on your computer, but they are internally stored and manipulated as a sequence of 0s and 1s.<\/p>\n
In Python, a string is a set of Unicode characters. Unicode was designed to include every character in every language and to introduce encoding uniformity to the world. Python Unicode will tell you all about Unicode you need to know.<\/p>\n
Example:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Given a string, the task is to convert each word first letter to uppercase<\/p>\n There are several ways to capitalize the first letter of each word in a string some of them are:<\/p>\n Syntax:<\/strong> given_string.capitalize() Approach:<\/strong><\/p>\n Below is the implementation of above approach:<\/strong><\/p>\n Output:<\/strong><\/p>\n Before returning a new string, the title() function in Python converts the first character in each word to Uppercase and the remaining characters in the string to Lowercase.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Using the spilt() method in Python, the string capwords() method capitalises all of the words in the string.<\/p>\n Break the statement into words, capitalise each word with capitalise, and then join the capitalised words together with join. If the optional second argument sep is None or missing, whitespace characters are replaced with a single space and leading and trailing whitespace is removed.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We’ll use regex to find the first character of each word and convert it to uppercase.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n A sequence of characters is referred to as a string. Characters are not used by computers instead, numbers are used (binary). Characters appear on your computer, but they are internally stored and manipulated as a sequence of 0s and 1s. In Python, a string is a set of Unicode characters. Unicode was designed to include …<\/p>\nstring = \"this is btech geeks\"<\/pre>\n
This Is Btech Geeks<\/pre>\n
Capitalize the First Letter of Each Word in a String<\/h2>\n
\n
Method #1:Using capitalize() function<\/h3>\n
\nParameters<\/strong>: No parameters will be passed
\nReturn :<\/strong> Each word’s first letter is capitalised in the string.<\/p>\n\n
# given string\r\nstring = \"this is btech geeks\"\r\n# convert the string to list and split it\r\nwordslist = list(string.split())\r\n# Traverse the words list and capitalize each word in list\r\nfor i in range(len(wordslist)):\r\n # capitizing the word\r\n wordslist[i] = wordslist[i].capitalize()\r\n# converting string to list using join() function\r\nfinalstring = ' '.join(wordslist)\r\n# printing the final string\r\nprint(finalstring)\r\n<\/pre>\n
This Is Btech Geeks<\/pre>\n
Method #2:Using title() function<\/h3>\n
\n
# given string\r\nstring = \"this is btech geeks\"\r\n# using title() to convert all words first letter to capital\r\nfinalstring = string.title()\r\n# print the final string\r\nprint(finalstring)<\/pre>\n
This Is Btech Geeks<\/pre>\n
Method #3:Using string.capwords() function<\/h3>\n
\n
# importing string\r\nimport string\r\n# given string\r\ngivenstring = \"this is btech geeks\"\r\n# using string.capwords() to convert all words first letter to capital\r\nfinalstring = string.capwords(givenstring)\r\n# print the final string\r\nprint(finalstring)\r\n<\/pre>\n
This Is Btech Geeks<\/pre>\n
Method #4:Using Regex<\/h3>\n
# importing regex\r\nimport re\r\n\r\n# function which converts every first letter of each word to capital\r\n\r\ndef convertFirstwordUpper(string):\r\n # Convert the group 2 to uppercase and join groups 1 and 2 together. \r\n return string.group(1) + string.group(2).upper()\r\n\r\n\r\n# given string\r\nstring = \"this is btech geeks\"\r\n\r\n# using regex\r\nresultstring = re.sub(\"(^|\\s)(\\S)\", convertFirstwordUpper, string)\r\n# print the final string\r\nprint(resultstring)\r\n<\/pre>\n
This Is Btech Geeks<\/pre>\n
\n