How to Convert List to Capitalize First Letter in Django?

Published On: 16/08/2022 | Category: Django


Hi Dev,

Here, I will show you how to works django list convert to capitalize first letter. it's simple example of capitalize the first letter of each word. I explained simply about python list convert to title case. This post will give you simple example of convert list to title case django.

There are many ways to convert list elements to capitalize first letter in django. i will give you two examples using for loop with title() to convert list data to capitalize first letter. so let's see the below examples.

You can use these examples with django3 (Django 3) version.

let's see below a simple example with output:

Example : 1 views.py
from django.shortcuts import render
from django.http import HttpResponse

def index(request):

    myList = ['one', 'two', 'three']
  
    # Convert List Value into capitalize
    for i in range(len(myList)):
        myList[i] = myList[i].title()
      
    print(myList)
    
    return HttpResponse(myList)
Output
['One', 'Two', 'Three']
Example : 2 views.py
from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    
    myList = ['one', 'two', 'three']
  
    # Convert List Value into capitalize
    newList = [x.title() for x in myList]
      
    print(newList)
    
    return HttpResponse(newList)
Output
['One', 'Two', 'Three']

I Hope It will help you....