-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13_find_first_substring.py
33 lines (30 loc) · 1.11 KB
/
13_find_first_substring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Please write a program which asks the user to type in a string and a single character. The program then prints
# the first three character slice which begins with the character specified by the user. You may assume the input
# string is at least three characters long. The program must print out three characters, or else nothing.
#
# Pay special attention to when there are less than two characters left in the string after the first occurrence
# of the character looked for. In that case nothing should be printed out, and there should not be any indexing
# errors when executing the program.
# Sample output
#
# Please type in a word: mammoth
# Please type in a character: m
# mam
# Sample output
#
# Please type in a word: banana
# Please type in a character: n
# nan
# Sample output
#
# Please type in a word: tomato
# Please type in a character: x
# Sample output
#
# Please type in a word: python
# Please type in a character:
string = input("Please type in a word: ")
character = input("Please type in a character: ")
index = string.find(character)
if index != -1 and index < len(string) - 2:
print(string[index:index + 3])