You can use this assignment in your class!
Mastery Progress(100.0%)
i

Write a function that returns if the nth character in a given string is a given character.

Parameters
s: the string to test if the nth character is c.
n: the number character in test_string to compare to c. (NOTE: n does NOT represent the 0-based index in the string. Rather, it represents the 1-based index in the string.
c: the character to compare with the nth character in s.

Output
Returns True if the nth character in s is c.

STDIN format
s n c (for instance, cat 2 a)

Sample input / outputs
cat 2 a / True
cat 1 a / False
1234 4 4 / True
1234 4 5 / False

You answeredJUST NOW
Python
def n_is_c(s, n, c):
# YOUR CODE HERE
return False # change this
# Input handling (do not edit)
parts = input().split(" ")
print(n_is_c(parts[0], int(parts[1]), parts[2]))
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
You passed all test cases!
STDIN
cat 2 a
Expected STDOUT
True
Your STDOUT
True
STDIN
cat 1 a
Expected STDOUT
False
Your STDOUT
False
STDIN
1234 4 4
Expected STDOUT
True
Your STDOUT
True
STDIN
1234 4 5
Expected STDOUT
False
Your STDOUT
False
STDIN
1234 -1 1
Expected STDOUT
False
Your STDOUT
False
STDIN
apple 1 a
Expected STDOUT
True
Your STDOUT
True
STDIN
apple 0 a
Expected STDOUT
False
Your STDOUT
False
STDIN
aaa -1 a
Expected STDOUT
False
Your STDOUT
False

Did you like this question?

(Voting helps us personalize your learning experience!)
imgInstructor solution
Alec KretchJAN 2, 2021, 5:59:42 PM
Python
def n_is_c(s, n, c):
if n > len(s) or n < 0: # if n is beyond the limits of the string, return False (avoid array bounds error)
return False
return s[n-1] == c # n-1 is the desired index
# Input handling (do not edit)
parts = input().split(" ")
print(n_is_c(parts[0], int(parts[1]), parts[2]))
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Was this helpful?

(Voting helps us personalize your learning experience!)
imgStrings in Python Presentation (page 2)

Was this helpful?

(Voting helps us personalize your learning experience!)
Think you've got it?

Which of the following options returns whether or not the last character of s is c?

Select one of the following options:
  • A.

    return s[len(s)] == c

  • B.

    return s[len(s)-1] == c

  • C.

    return s[-1] == c

  • D.

    A & C

  • E.

    B & C

Submit answer

Was this helpful?

(Voting helps us personalize your learning experience!)

You may exit out of this review and return later without penalty.