简体   繁体   中英

I am new to python, help me understand this, one line of a code?

def translate(phrase):
    translation = ""
    for letter in phrase:
        if letter.lower() in "aeiou":
            if letter.isupper():
                translation = translation + "G"
            else:
                translation = translation + "g"
        else:
            translation = translation + letter
    return translation


print(translate(input("Enter a phrase: ")))

the rest of the codes is okay to me. I guess. except this one. I don't understand

if letter.low() in "aeiou":

like how do you read it?

if letter is lowercase in "aeiou" ? does the "in" stand for something else?

Sorry, I just started learning python 5 days ago.

letter.lower() converts letter to its lowercase equivalent.

in is used to test if something is a member of a collection or sequence. In this case, the sequence is the characters in the string "aeiou" .

So this tests if the lowercase version of letter is one of the characters in that string, ie it tests if letter is a vowel.

In the line if letter.low() in "aeiou":

First of all you convert the letter(it can be in uppercase or lowercase) to lowercase. Then you check that if it is in "aeiou" or not ,if the letter is there in "aeiou" then next if statement will get executed.

[left] in [right]

says run a search for [left] in side [right]. If it is found then [left] in [right] means true. otherwise it means false.

if [condition] :
  [action]

says perform action if [condition] is true.

letter.lower() as you found means give me the lowercase version.

Basically the code appears to be re-building a string letyer by letter. It will add the letter if the letter is not a vowel, and add G or g to the case of the vowel otherwise.

It determines whether something is a vowel via checking it against a group containing what it considers vowels.

it's just converting to lowercase because all the vowels it's checking for are in lowercase. If you change what you're checking for, you can remove converting to lowercase and it'll work exactly the same:

if letter in "aeiouAEIOU":

When you do an in check in Python you are checking to see if the 'thing' you are checking for is in some collection of other things. Since you just started learning we will use strings as an example of a "collection of things" but know that it can be extended in python for any collections type: lists, tuples, dictionaries, and some more.

when you ask python to do a check:

if "a" in "aA":
    print("exists in string")
else:
    print("does not exist in string")

you are asking if a (the thing you are searching for) occurs at least once in the string (which is the collection in this case) of aA . Because it does, the above code will print: exists in string .

Note that we can do an in member check for more than just one character strings:

if "aa" in "aaB":
    print("yes")
else:
    print("no")

will also print yes since 'aa' does indeed occur in the string 'aaB' . This only for exact matches though. Consider:

if "aa" in "aBaB":
    print("yes")
else:
    print("no")

will print 'no' since the exact string 'aa' does not occur in 'abab' even though we have 2 a 's.

the .lower() function the string in the if statement simply lowercases the entire string first before doing the check.

if letter.lower() in "aeiou":将输入标识为小写 aeiou

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM