How to Convert String into List in Django?
Published On: 26/07/2022 | Category:
Django
Python
Hi Dev,
This post will give you example of django string into list. In this article, we will implement a how to convert string into list in django. let’s discuss about how to turn string into list in django. you'll learn how to convert string into list of words in django.
In this example, there are several ways to convert strings into a list in python. we will use split() and strip() functions to convert string into list. so let's see the below examples.
You can use these examples with django3 (django 3) version.
let's see below simple example with output:
Example : 1 views.pyfrom django.shortcuts import render from django.http import HttpResponse def index(request): myString = "Tuts-Station.com is a best site!" # Convert String into List newList = myString.split(" ") print(newList) return HttpResponse('')Output
['Tuts-Station.com', 'is', 'a', 'best', 'site!']Example : 2 views.py
from django.shortcuts import render from django.http import HttpResponse def index(request): myString = "One,Two,Three,Four,Five" # Convert String into List newList = myString.split(",") print(newList) return HttpResponse('')Output
['One', 'Two', 'Three', 'Four', 'Five']Example : 3 views.py
from django.shortcuts import render from django.http import HttpResponse def index(request): myString = "Site Tuts-Station.com" # Convert String into List newList = list(myString.strip(" ")) print(newList) return HttpResponse('')Output
['S', 'i', 't', 'e', ' ', 'T', 'u', 't', 's', '-', 'S', 't', 'a', 't', 'i', 'o', 'n', '.', 'c', 'o', 'm']
I Hope It will help you....