I am new to programming and recently started learning strings and loops. I want to know is it possible to assign the index of any alphabet from a string to a variable?
def max_char(a_string):
val="abcdefghijklmnopqrstuvwxyz"
counter=0
for i in range (len(val)):
for k in range(len(a_string)):
if val[i] == a_string[k]:
if counter==0:
x1=val.index(i) #here I want to assign the index of ith element of val to x1. Also tried *x1=val[i].index*
counter=1
CodePudding user response:
you can assign it by using the index itself that you are declaring in the loop.
x1=i #This is the index of val in that iteration.
In this case this code will return the first element of a_string that is also in val.
CodePudding user response:
Just assign to i
def max_char(a_string):
val = "abcdefghijklmnopqrstuvwxyz"
counter = 0
for i in range(len(val)):
for k in range(len(a_string)):
if val[i] == a_string[k]:
if counter == 0:
x1 = i # here I want to assign the index of ith element of val to x1. Also tried *x1=val[i].index*
counter = 1
print(x1)
max_char("s")
Output: 18