Write a function that takes a string and a character, s and char as arguments and returns the index of the character where it first appears in the string. It the char doesn't appear in the string, it returns -1
Write a function that takes two values and returns True if they are equal, or False otherwise.
Write a function, that takes a word and a character,
word and char, and returns the index of char where it appears in word.
If there are more than one char, it should return the last index.
Example:
>>> last('ani', 'n')
1
>>> last('nana', 'n')
2
>>> last('nana', 'a')
3
Restrictions: You cannot use +, - or ==
Write a function which takes two strings, s1 and s2, and it concatenates the first 3
characters of s1 with the last 2 characters of s2 and returns it.
Example:
>>> concatenate_strings("power", "owl")
'powwl'
Define a function that takes a string and returns True if its third character is 'q'.
Write a function which takes a string, s, as argument, and prints only its characters with index less than 5 (not including 5).
Encapsulate this code: word = 'banana'
count = 0
for letter in word:
if letter == 'a':
count = count + 1
print(count)
in a function named count, and generalize it so that it accepts the string and the letter as arguments.
Result should be printed, not returned
def get_length(s):
return len(s)
def get_indices(s):
return range(get_length(s))
def concat(s, n):
w = ""
for i in get_indices(s):
if i % n == 0:
w = w + s[i]
return w
Given the above script, what are the results of the following expressions:
| concat('Whimsy', 2): | ||
| concat('Serendipity', 3): |
def ani(word, char):
index = 0
t = 0
while index < len(word):
if word[index] == char:
t = t + index
index = index + 1
return t
def add(word, ch1, ch2):
return ani(word, ch1) + ani(word, ch2)
Given the above script, what are the results of the following expressions:
| add('kungfu panda', 'k', 'u'): | ||
| add('kungfu panda', 'f', 'g'): |
Complete execution flow of the following program
def in_both(word1, word2): for letter in word1: if letter in word2: print(letter)
Complete execution flow of the following program
def ani(word, char): index = 0 t = 0 while index < len(word): if word[index] == char: t = t + index index = index + 1 return t def add(word, ch1, ch2): return ani(word, ch1) + ani(word, ch2) d = add('sop', 'o', 'p')