returns the position of the string character from the character list with Python
you need to find the position of each character in the string from the list of characters, and then return the entire character position to a single string, with the exception of the last space between each character position. You need to ignore all characters that are not in the character list that are composed of the characters in AZ.
Strategy:
First, we need to declare a list of the AZ characters in the list.
Alphabetlist = ["A", ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' Z ']
Next, create a loop that iterates through each character in the string, finds the position of each character in the list of characters above, skips the iteration and goes to the next character if the character is not in the list. We need to make sure that the characters have been converted to lowercase before the start of the search, and to the return index containing 0 (we want our index to start at 1 instead of 0) plus 1.
For ch in text:if (Ch.lower () No in alphabetlist): Continue else:ch = Ch.lower () Letterpos + = str (alphabetlist.index (CH) + 1) + ""
Finally, return the string position, and don't forget to truncate the space at the end of the string. The complete code is as follows:
Def alphabet_position (text): if (len (text) == 0): # if the text is blank then return it Return text alphabetlist = ["A", ' B ', ' C ', ' d ', ' E ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' O ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' W ', ' x ', ' y ', ' z ' letterpos = ' for ch in text: if (Ch.lower () not in alphabetlist): continue else: ch = ch.loweR () letterpos += str ( Alphabetlist.index (CH) + 1) + " " return letterpos.rstrip ()
Now we can call the above function with the string argument to see the result.
Returns the position of the string character from the character list with Python