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 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.
Example:
Input:
string = "this is btech geeks"
Output:
This Is Btech Geeks
Given a string, the task is to convert each word first letter to uppercase
Capitalize the First Letter of Each Word in a String
There are several ways to capitalize the first letter of each word in a string some of them are:
Method #1:Using capitalize() function
Syntax: given_string.capitalize()
Parameters: No parameters will be passed
Return : Each word’s first letter is capitalised in the string.
Approach:
- Because spaces separate all of the words in a sentence.
- We must use split to divide the sentence into spaces ().
- We separated all of the words with spaces and saved them in a list.
- Using the for loop, traverse the wordslist and use the capitalise feature to convert each word to its first letter capital.
- Using the join function, convert the wordslist to a string.
- Print the string.
Below is the implementation of above approach:
# given string string = "this is btech geeks" # convert the string to list and split it wordslist = list(string.split()) # Traverse the words list and capitalize each word in list for i in range(len(wordslist)): # capitizing the word wordslist[i] = wordslist[i].capitalize() # converting string to list using join() function finalstring = ' '.join(wordslist) # printing the final string print(finalstring)
Output:
This Is Btech Geeks
Method #2:Using title() function
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.
- Syntax:Â string_name.title()
- Parameters: string in which the first character of each word must be converted to uppercase
- Return Value: Each word’s first letter is capitalised in the string.
Below is the implementation:
# given string string = "this is btech geeks" # using title() to convert all words first letter to capital finalstring = string.title() # print the final string print(finalstring)
Output:
This Is Btech Geeks
Method #3:Using string.capwords() function
Using the spilt() method in Python, the string capwords() method capitalises all of the words in the string.
- Syntax: string.capwords(given_string)
- Parameters: The givenstring that needs formatting.
- Return Value: Each word’s first letter is capitalised in the string.
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.
Below is the implementation:
# importing string import string # given string givenstring = "this is btech geeks" # using string.capwords() to convert all words first letter to capital finalstring = string.capwords(givenstring) # print the final string print(finalstring)
Output:
This Is Btech Geeks
Method #4:Using Regex
We’ll use regex to find the first character of each word and convert it to uppercase.
Below is the implementation:
# importing regex import re # function which converts every first letter of each word to capital def convertFirstwordUpper(string): # Convert the group 2 to uppercase and join groups 1 and 2 together. return string.group(1) + string.group(2).upper() # given string string = "this is btech geeks" # using regex resultstring = re.sub("(^|\s)(\S)", convertFirstwordUpper, string) # print the final string print(resultstring)
Output:
This Is Btech Geeks
Related Programs:
- capitalize first letter of each word in a string in java
- python program to read a file and capitalize the first letter of every word in the file
- convert first letter of each word of a string to upper case in cpp
- how to uppercase the first letter of a string in javascript
- check the first or last character of a string in python
- python program to remove the ith occurrence of the given word in a list where words can repeat
- python program to sort a list of tuples in increasing order by the last element in each tuple