0

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

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.