What capitalize () do in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Python String capitalize() method returns a copy of the original string and converts the first character of the string to a capital (uppercase) letter, while making all other characters in the string lowercase letters.

    Python String capitalize() Method Syntax

    Syntax: string_name.capitalize()

    Parameter:  The capitalize() function does not takes any parameter. 

    Return: The capitalize() function returns a string with the first character in the capital.

    Python String capitalize() Method Example

    Python

    name = "geeks FOR geeks"

    print(name.capitalize())

    Output:

    Geeks for geeks

    Example 1: capitalize() only capitalizes the first letter of a string and lowers all the remaining characters

    Python3

    string = "roses are red"

    print("Original string:", string)

    print("After using capitalzie:", string.capitalize())

    Output:

    Original string: roses are red
    After using capitalzie: Roses are red

    Example 2: Python String capitalize() Method Doesn’t Modify the Original String

    Python String capitalize() Method creates and returns a copy of the original string after modifications.

    Python3

    string = "geeks for geeks"

    string_2 = string.capitalize()

    print("New string after using capitalize():", string_2)

    print("Original string:", string)

    Output:

    New string after using capitalize(): Geeks for geeks
    Original string: geeks for geeks

    ❮ String Methods


    Example

    Upper case the first letter in this sentence:

    txt = "hello, and welcome to my world."

    x = txt.capitalize()

    print (x)

    Try it Yourself »


    Definition and Usage

    The capitalize() method returns a string where the first character is upper case, and the rest is lower case.


    Syntax

    Parameter Values

    No parameters


    More Examples

    Example

    The first character is converted to upper case, and the rest are converted to lower case:

    txt = "python is FUN!"

    x = txt.capitalize()

    print (x)

    Try it Yourself »

    Example

    See what happens if the first character is a number:

    txt = "36 is my age."

    x = txt.capitalize()

    print (x)

    Try it Yourself »


    ❮ String Methods


    Strings are one of the most used python data structures. While programming in python, some strings are used in uppercase while some are in lowercase and some in combination. Hence, it is chaotic to pay attention to every string you create and use while programming, and also quite difficult to correct it manually.

    As it is important and default format to capitalize the first letter of every word for readers convenience, we have presented a detailed article representing 8 different methods to capitalize the first letter in python and the examples. But, before learning those methods, let us have a brief introduction to strings in python.

    What are Strings in Python?

    Python string is the collection of the characters surrounded by single quotes, double quotes, or triple quotes. The computer does not understand the characters; internally, it stores manipulated characters as the combination of the 0's and 1's. This means that strings can be parsed into individual characters and that individual characters can be manipulated in various ways.

    This is what makes Python so versatile: you can do almost anything with it, and some things even work the way they do in another language. To learn more about strings in python, refer to our article "4 Ways to Convert List to String in Python".

    For Example

    Output

    How to Capitalize the First Letter in Python?

    Below are the eight methods to capitalize the first letter of strings in python:

    1) Using str.capitalize() to capitalize first letter

    The string.capitalize () function takes the string argument and returns the first letter of the capitalized word. This can be useful if you have a lot of text that you want to format as uppercase automatically or if you want to change the file's name or folder. The function works on any string containing English letters, numbers, and punctuation marks.

    For Example

    s = "python"
    print("Original string:")
    print(s)
    print("After capitalizing first letter:")
    print(s.capitalize())
    

    Output

    Original string:
    python
    After capitalizing first letter:
    Python
    

    2) Using string slicing() and upper() method

    We used the slicing technique to extract the string’s first letter in this method. We then used the upper() method of string manipulation to convert it into uppercase. This allows you to access the first letter of every word in the string, including the spaces between words. The below example shows the working of this method in detail.

    For Example

    s = "python"
    print("Original string:")
    print(s) 
    result = s[0].upper() + s[1:]
    print("After capitalizing first letter:")
    print(result)
    

    Output

    Original string:
    python
    After capitalizing first letter:
    Python
    

    3) Using str.title() method

    string.title() method is a very simple and straightforward method of generating titles for strings. As the titles for string have a default structure where the first letter is always in upper case, this method helps us capitalize the first letter of every word and change the others to lowercase, thus giving the desired output. This method is also useful for formatting strings in HTML and formatting strings in JavaScript and other programming languages.

    For Example

    s = "python"
    print("Original string:")
    print(s)
    print("After capitalizing first letter:")
    print(str.title(s))
    

    Output

    Original string:
    python
    After capitalizing first letter:
    Python
    

    When we use the entire sentence as the input string, it will capitalize the first letter of every word in the string, as shown below.

    For Example

    s = "it's DIFFICULT to identify 10a "
    # Capitalize the first letter of each word
    result = s.title()
    print(result)
    

    Output

    It isn't easy To Identify 10A
    

    The behaviors drawn from the above example are:

    1. “DIFFICULT” is converted into “Difficult” because the title function only capitalizes the first letter of every word and keeps the remaining characters of the word as lower case.
    2. “it's” is converted to "It'S” because the function considers “it's” as two separate words by considering apostrophes.
    3. “10a” is converted to “10A” because the title function considers “as the first character for the word “a."

    4) Using capitalize() function to capitalize the first letter of each word in a string

    Here, we make use of the split() method to split the given string into words. The generator expression iterates through the words, using the capitalize() method to convert the first letter of each word into uppercase. The capitalize() method converts each word’s first letter to uppercase, giving the desired output. The below example shows the working of the capitalize function in detail.

    Example

    s = "python"
    print("Original string:")
    print(s)
    print("After capitalizing first letter:")
    result = ' '.join(elem.capitalize() for elem in s.split())
    print(result)
    

    Output

    Original string:
    python
    After capitalizing first letter:
    Python
    

    5) Using string.capwords()

    capwords() is a python function that converts the first letter of every word into uppercase and every other letter into lowercase. The function takes the string as the parameter value and then returns the string with the first letter capital as the desired output. Check out the below example to understand the working of the capwords() function.

    For Example

    import string
    s = "python"
    print("Original string:")
    print(s)
    print("After capitalizing first letter:")
    result = string.capwords(s)
    print(result)
    

    Output

    Original string:
    python
    After capitalizing first letter:
    Python
    

    6) Using regex to capitalize the first letter in python

    Regex is generally known as a regular expression in python, is a special sequence of characters that helps match or find the other strings. Using regex, you can search the starting character of each word and capitalize it. For using this method, you have to import the regex library using the “import” keyword before defining the main function, as shown in the below example. Also, remember that this method only capitalizes the first character of each word in python and does not modify the whitespaces between the words.

    For Example

    import re
    def convert_into_uppercase(a):
        return a.group(1) + a.group(2).upper()
    s = "python is best programming language"
    result = re.sub("(^|\s)(\S)", convert_into_uppercase, s)
    print(result)
    

    Output

    Python Is Best Programming Language
    

    7) Capitalize the first letter of every word in the list

    You must wonder how difficult it would be if we had an entire list of words as a string instead of a single string to capitalize the first letter in python. Well, it is quite simple. When you have an entire list of words and wish to capitalize the first letter of every word, you can iterate through the words in the list using for loop and then use the title() method in python. This process will help you to convert the first letter of each word in the list to uppercase.

    For Example

    fruits=['apple','grapes','orange']
    print('Original List:')
    print(fruits)
    fruits = [i.title() for i in fruits]
    print('List after capitalizing each word:')
    print(fruits)
    

    Output

    Original List:
    ['apple', 'grapes', 'orange']
    List after capitalizing each word:
    ['Apple', 'Grapes', 'Orange']
    

    8) Capitalize the first letter of each word in the file

    Capitalizing the first letter of any given the word manually is quite feasible, but what if you have to capitalize the first letter of every word in any given file? Well, it is quite easy too. For this situation, you have to use the open() method to open the file in the reading mode and then iterate through every word using for loop. Later, you can capitalize the first letter of every word using the title() function, just like shown in the below example.

    For Example

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

    Output

    Hello! Welcome To Favtutor
    

    Conclusion

    As strings are frequently used data structure while programming in python, it is not feasible to capitalize the first letter of each string word manually. Hence, we have presented various ways to convert the first letter in the string as uppercase. All these functions draw the same output, and hence you can choose the one according to your need. However, if you face any difficulty, get in touch with our python tutors to solve your doubts.

    What is the difference between title () and capitalize ()?

    The difference between them is that Python string method title() returns a copy of the string in which the first characters of all the words are capitalized whereas the string method capitalize() returns a copy of the string in which just the first word of the entire string is capitalized.

    How do you capitalize letters in Python?

    In Python, upper() is a built-in method used for string handling. The upper() method returns the uppercased string from the given string. It converts all lowercase characters to uppercase. If no lowercase characters exist, it returns the original string.

    How do you capitalize all text in Python?

    Performing the . upper() method on a string converts all of the characters to uppercase, whereas the lower() method converts all of the characters to lowercase.