
Available under Creative Commons-NonCommercial-ShareAlike 4.0 International License.
The word in is a boolean operator that takes two strings and returns True if the first ap pears as a substring in the second:
>>> 'a' in 'banana'True>>> 'seed' in 'banana'False
For example, the following function prints all the letters from word1 that also appear in word2:
def in_both(word1, word2): for letter in word1: if letter in word2: print letter
With well-chosen variable names, Python sometimes reads like English. You could read this loop, “for (each) letter in (the first) word, if (the) letter (appears) in (the second) word, print (the) letter.”
Here’s what you get if you compare apples and oranges:
>>> in_both('apples', 'oranges')aes
- 瀏覽次數:1668