Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string starts with any letter between 'A' and 'G' in django template

Tags:

python

django

How can I check if a string starts with 'A' or 'B' or 'C'.... or 'G' in django template.

I've this method presently. Can it be done in a better way?

{% if val|slice:":1" == 'A' or val|slice:":1" == 'B' or val|slice:":1" == 'C'.... or val|slice:":1" == 'G' %}
like image 208
sunnyiitkgp Avatar asked Feb 07 '23 18:02

sunnyiitkgp


1 Answers

Strings are treated like sequences in Python so you can check membership like so:

{% if val|first in 'ABCG' %}
    # Do stuff
{% endif %}
like image 138
Vasili Syrakis Avatar answered Apr 27 '23 20:04

Vasili Syrakis