Python capitalize first letter of every word in string (8 Ways)

remove none from list Python

In this, we will be solving a problem statement of python capitalize first letter of string. We will be discussing the steps and python methods required to solve this problem.

Strings in Python are one of the most used data structures. While working with strings, the string can be in uppercase, lowercase, or in any other random format. Hence it is difficult to process any random strings. Therefore, we should convert stings into a fixed format that can be easily processed.

Python capitalize first letter in string using str.capitalize() method

The easiest method to capitalize first letter of string in python is by using string capitalize() method. The capitalize() method converts the first character of the string in uppercase, and the rest other characters in lowercase.

Syntax

str_name.capitalize()

Parameter – It doesn’t take any parameter

Return Value – Capitalize method returns the copy of the string which the first letter as capital.

Python Code:

str_name = "python"
print("After using capitalize method -> ",str_name.capitalize())
str_name1 = "python tutorial"
print("After using capitalize method -> ",str_name1.capitalize())

Output:-

After using capitalize method ->  Python
After using capitalize method ->  Python tutorial

Using slicing and upper() method to capitalize first letter in string

Another way to capitalize first letter in string is by using string slicing and the string upper() method. To know more about string slicing you can read here.

Steps to follow

  1. Separate the first character and the rest of the characters using string slicing.
  2. Make the first character capital using the string upper() method
  3. Concatenate the first character which is a capital letter now with the rest of the characters.

Python Code:

str_name = "python"
first_letter = str_name[0]
rest_of_charcters = str_name[1:]
capitalize_string = str_name[0].upper() + rest_of_charcters
print("After using capitalize method,", str_name,"becomes ->",capitalize_string)

Output:-

After using capitalize method, python becomes -> Python

Also Read:

Using title() method to capitalize first letter of every word in string

The easiest way to capitalize the first letter of every word in a string is by using the python string title() method. The string title() method returns a copy of the string in which the first character of each word of the sentence is upper case.

Syntax

str_name.title()

Parameter – It doesn’t take any parameter

Return Value – A string, in which the first character of each word is capitalized.

Python Code:

str_name = "my programming tutorial"
print("After using capitalize method -> ",str_name.title())
str_name1 = "python tutorials"
print("After using capitalize method -> ",str_name1.title())

Output:-

After using capitalize method ->  My Programming Tutorial

After using capitalize method ->  Python Tutorials

Using string.capwords() method to capitalize first letter of every word in string

Another way to capitalize the first letter of every word in string python is by using string.capwords() method. In Python, the string capwords() method is used to capitalize all words in a string using the spilt() method.

The Python String capwords() method splits the string using the string split() method. Capitalize each word of the string using the string capitalize() method and join the capitalized words using the join method.

Syntax

 string.capwords(str_name, sep=None)

Parameters

  1. Input string
  2. Separator – The character which is used to split the given string. The default value of the separator is None.

Return Value – A string, in which the first character of each word is capitalized.

Python Code:

import string
str_name = "my programming tutorial"
print("After using capitalize method -> ",string.capwords(str_name))
str_name1 = "python tutorials"
print("After using capitalize method -> ",string.capwords(str_name1))

Output:-

After using capitalize method -> My Programming Tutorial
After using capitalize method -> Python Tutorials

Using split, capitalize and join methods to capitalize first letter of string python

In this method, we will perform the following steps-

  • Split the given string, using the string split() method, which returns the list of words present in the string.
  • Capitalize every word in the list using capitalize method().
  • And at last, concatenate the words using the join method.

Python Code:

str_name = "my programming tutorial"
capitalize_string = ' '.join([word.capitalize() for word in str_name.split()]) 
print("After Capitalize ->", capitalize_string)

Output:-

After Capitalize -> My Programming Tutorial

Also Read: 4 ways to count occurrences in list Python

Using Regex to capitalize first letter of every word in string

In Python Regular Expression or Regex is a special sequence of characters that helps to match or find the other strings. To know more about regex in python you can read here.

Python Code:

# importing re module
import re
# return uppaercase character of every word. 
def uppercase_first_character(g):
    return g.group(1) + g.group(2).upper()
str_name = "my programming tutorial"
capitalize_string = re.sub("(^|s)(S)", uppercase_first_character, str_name)
print("After using capitalizing using regex -> ", capitalize_string)

Output:-

After using capitalizing using regex -> My Programming Tutorial

How to capitalize every word in list

To capitalize every word in the list, we can use list comprehension. Use for loop to access each word in the list and then use the string title() method to capitalize the word. To know more about list comprehension you can read here.

Python Code:

sample_list = ["my", "programming", "tutorial"]
capitalize_list = [word.title() for word in sample_list]
print("Capitalize words List",capitalize_list)

Output:-

Capitalize words List ['My', 'Programming', 'Tutorial']

How to capitalize each word in file

To manually capitalize every word in any given file is not convenient. It is very easy by using python. We can do this by each reading file line by line and using the string title() method on each line.

Python Code:

file = file = open(any_file.txt', 'r')
for line in file:
	print(line.title())

Output:-

Welcome To My Programming Tutorial.!

Conclusion

Hence we have discuss python capitalize first letter in string. We can do this by using python methods like the title(), string.capwords(), capitalize(). We can use regex, list comprehension, and string slicing to capitalize each word in string in python.

Similar Posts