0

list1 is a list here in python and i is some constant like 7(integer).


Look at the snippet of code below :-

for j in range(i, len(list1)):
  if list1[j][0] == 'Ġ' or '.' or '!' or '?' or ',':
    #do something

When I execute the above code only 'Ġ' is considered for checking whether the list1 ith string's first character is 'Ġ' or not and the rest '.','!','?'and ',' are ignored.


I also thought of using the startswith() fucntion like this :-

for j in range(i, len(list1)):
      if list1[j].startswith([Ġ!?.,]):
        #do something

But then the above code also wasn't working properly, because I realized that startswith() function doesn't accept regular expression. Also, it accepts only one character so that implies that I can't add parameters like this :-

if list1[j].startswith('Ġ' or '.' or '!' or '?' or ','):

Does anybody know of what could be done here? Please help. I want to check whether the first character of the string belongs to any of these character - 'Ġ', '.', '!', '?', ','

Neha Chaudhary
  • 1,071
  • 4
  • 15
  • 31

1 Answers1

1

Use:

if list1[j][0] in "Ġ.!?,":
    ... 
donkopotamus
  • 22,114
  • 2
  • 48
  • 60