Capitalize first letter of a string in Python (5 Solutions)
Python is a programming language with many features and functions. One of these features is the ability to capitalize the first letter of a string. This can be done with a few different methods, all of which are relatively simple to understand and implement. In this article, we will go over how to capitalize the first letter of a string in Python.
my_str = "python is a programmming language"
result = my_str.capitalize()
print(result)
# Output
# Python is a programmming language
Solution 1: Capitalize first letter of string using capitalize() function
In Python, the capitalize() function can be used to capitalize the first letter of a string. This is useful when you want to make sure that the first letter of a string is always capitalized, such as when you are printing a name or a title. To use the capitalize() function, simply specify the string that you want to capitalize as the argument. For example, if you want to capitalize the string "python", you would write: "python".capitalize().
Syntax
String.capitalize()
Code example 1
my_str = "foo bar"
result = my_str.capitalize()
print(result)
Output
Foo bar
This code example capitalizes the first letter of the string "foo bar". The result is "Foo bar".
Code example 2
my_str = "proGrAmming is Fun"
result = my_str.capitalize()
print(result)
Output
Programming is fun
The capitalize() function will convert the first letter of the string to Uppercase and all the other letters will be lowercase.
Solution 2: Using upper() function and string index
Python has a built-in function called upper() which converts a string into upper case. There is also a string index that can be used to access individual characters in a string. By combining these two features, it is possible to capitalize the first letter of a string. We will understand it using below code example.
Syntax
input_str[0].upper() + input_str[1:]
Code example
my_str = "devsheet is for programmers"
result = my_str[0].upper() + my_str[1:]
print(result)
Output
Devsheet is for programmers
This code example is using the string method upper() to make the first letter of the string uppercase, and then concatenates the rest of the string to it.
Solution 3: Capitalize the first letters of every word in a String using title() function
The String.title() function in Python is used to capitalize the first letter of each word in a string. This is a built-in function, so you don't need to import anything to use it.
Syntax
String.title()
Code example
question = "how to make pizza"
result = question.title()
print(result)
# Output
# How To Make Pizza
Output
How To Make Pizza
The code example above shows how to make a string title() case using the title method. The title() method returns a string with each word capitalized.
Solution 4: Using capwords() function of Python string module
The capwords() function of the string module in Python can be used to capitalize each word in a string. This is useful when creating Headings or titles.
Python's string module provides a capwords() function which takes a string as an argument and returns a capitalized version of the string where each word is capitalized and all other characters are lowercase. This function is useful for creating readable, title-like strings.
Syntax
import string
string.capwords(input_str)
Code example
import string
my_str = "making the world a bEtter place"
result = string.capwords(my_str)
print(result)
Output
Making The World A Better Place
In the above code example
- The first line imports the string module.
- The second line creates a variable called my_str and assigns it the string "making the world a bEtter place".
- The third line creates a variable called result and assigns it the result of calling the capwords() function from the string module on the my_str variable.
- The fourth line prints the value of the result variable.