You are currently viewing Python Capitalize First Letter of String

How to capitalize the first letter of a string in Python? you can use strung capitalize() method to return a copy of the original string where the first character is converted to uppercase and the rest of the characters are converted to lowercase.

Advertisements

In this article, I will explain the Python string capitalize() method and slicing technique how we can capitalize the first letter of a string, and also explain using several methods like capitalize(), title(), upper(), split(), and string.capwords() functions how we can capitalize the first letter of each word in a string with examples.

1. Quick Examples of Capitalize First Letter of String

If you are in a hurry, below are some quick examples of how to capitalize the first letter of a string.


# Quick examples of capitalize first letter of a string

# Initialize the string 
string = "welcome to sparkbyexamples"

# Example 1: Using capitalize() method
# Capitalize the first letter of a string
capitalized_string = string.capitalize()

# Example 2: Using String slicing and upper() Method 
capitalized_string = string[0].upper() + string[1:]

# Capitalize the first letter of each word in a string 

# Example 3: Using title() method 
# Capitalize the first letter of each word in a string
capitalized_string = string.title()

# Example 4: Using String.capwords() 
capitalized_string = string.capwords(mystring)

# Example 5: Using split() and generator expression
capitalized_string =  ' '.join(elem.capitalize() for elem in string.split())

# Example 6: Capitalize the first letter of each word 
capitalized_string = [word.title() for word in string]

2. Usage of String capitalize() Method

The string.capitalize() method in Python that takes a string and returns a copy of the string with the first character capitalized and the rest of the characters in lowercase.

2.1. Syntax of String capitalize() Method

Following is the syntax of capitalize() method


# Syntax of capitalize() function
string.capitalize()

2.2 Parameter of capitalize()

The capitalize() method does not take any parameters.

2.3 Return Value

The capitalize() method is called directly on the string object and returns a new string with the first character capitalized.

3. Capitalize the First Letter of String using capitalize()

You can capitalize the first letter of a string using the capitalize() method. For example, the capitalize() method is applied to the string variable, it converts the first character of the string to uppercase while leaving the rest of the string unchanged. The resulting capitalized string is then printed to the console. In this case, it capitalizes the 'w' in "welcome" to 'W', resulting in "Welcome to sparkbyexamples".


# Initialize string 
string = "welcome to sparkbyexamples"
print("Original string:",string)

# Using capitalize() method
# Capitalize the first letter of a string
capitalized_string = string.capitalize()
print("The string after capitalizing the first letter:",capitalized_string)

Yields below output.

python string capitalize first letter

4. Using String slicing and upper() Method

You can use string slicing and the upper() method to capitalize the first letter of a string. For example, string[0] extracts the first character of the string, and string[1:] slices the remaining part of the string starting from the second character. The upper() method is then applied to the first character to convert it to uppercase. Finally, the capitalized first character and the rest of the string are concatenated to form the capitalized string.


# Initialize string 
string = "welcome to sparkbyexamples"
print("Original string:",string)

# Using String slicing and upper() Method 
capitalized_string = string[0].upper() + string[1:]
print("The string After capitalizing the first letter:",capitalized_string)

# Output:
# Original string: welcome to sparkbyexamples
# The string after capitalizing the first letter: Welcome to sparkbyexamples

5. Capitalize the First Letter of Each Word in a String

5.1 Using title()

You can capitalize the first letter of each word in a string using the title() method. For example, the title() method is applied to the string variable, it capitalizes the first letter of each word in the string, resulting in "Welcome To Sparkbyexamples". The resulting capitalized string is then printed to the console.


# Initialize string 
string = "welcome to sparkbyexamples"
print("Original string:",string)

# Using title() method 
# Capitalize the first letter of each word in a string
capitalized_string = string.title()
print("The string after capitalizing the first letters:",capitalized_string)

Yields below output.

python string capitalize first letter

5.2 Using string.capwords() Method

You can also capitalize the first letter of each word in a string using the capwords() function from the string module. For example, the capwords() method is called with the string as an argument. It capitalizes the first letter of each word in the string, resulting in "Welcome To Sparkbyexamples". The capitalized string is then printed to the console.

Note that you need to import the string module to use the capwords() function.


import string

# Initialize string 
mystring = "welcome to sparkbyexamples"
print("Original string:",mystring)

# Using String capwords() method 
capitalized_string = string.capwords(mystring)
print(" The string after capitalizing the first letters:",capitalized_string)

# Output:
# Original string: welcome to sparkbyexamples
# The string after capitalizing the first letters: Welcome To Sparkbyexamples

5.3 Using split() and Generator Expression

You can also capitalize the first letter of each word in a string using the split() method and a generator expression. For example, string.split()method splits the string into a list of words using spaces as the delimiter. The generator expression elem.capitalize() is used to capitalize the first letter of each elem. Finally, ''.join() joins the capitalized words back together with spaces.


# Initialize string 
string = "welcome to sparkbyexamples"
print("Original string:",string)

# Using split() and generator expression
capitalized_string =  ' '.join(elem.capitalize() for elem in string.split())
print(" The string after capitalizing the first letters:",capitalized_string)

Yields the same output as above.

5.4 Capitalize the First Letter of Each Word using title()

Another way to capitalize the first letter of each word in a string or a list of words, you can use the title() method in Python. For example, using list comprehension to iterate through each word of a string and then apply the title() method to capitalize the first letter of each word. The resulting capitalized string or list of words is then printed to the console.


# Initialize string 
string = ["welcome", "to", "sparkbyexamples"]
print("Original string:",string)

# Capitalize the first letter of each word 
capitalized_string = [word.title() for word in string]
print("The string after capitalizing the first letters:",capitalized_string)

# Output:
# Original string: ['welcome', 'to', 'sparkbyexamples']
# The string after capitalizing the first letters: ['Welcome', 'To', 'Sparkbyexamples']

6. Conclusion

In this article, I have explained the Python string capitalize() method and slicing technique how we can capitalize the first letter of a string and also explained using several methods like capitalize(), title(), upper(), split(), and string.capwords() functions how we can capitalize the first letter of each word in a string with examples.

Happy Learning !!