Returns the position of the string character from the character list with Python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.