forked from Zipcoder/PyPart5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils.py
More file actions
73 lines (50 loc) · 1.92 KB
/
string_utils.py
File metadata and controls
73 lines (50 loc) · 1.92 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def str_len(str_in: str) -> int:
"""
Given a string parameter, this function should return the length of the parameter.
"""
return len(str_in)
# str_len('Hello!')
def first_char(str_in: str) -> str:
"""
Given a string parameter, this function should return the first letter of the parameter.
"""
return str_in[0]
# first_char('Hello!')
def last_char(str_in: str) -> str:
"""
Given a string parameter, this function should return the last letter of the parameter..
"""
return str_in[len(str_in) - 1]
# last_char('Hello!')
def input_has_substring(str_in: str, sub_str_in: str) -> bool:
"""
This function determines if the substring exists within the string. Returns True or False.
"""
return sub_str_in in str_in
# input_has_substring('Hello!', 'ell')
def substring(str_in: str, start: int, stop: int) -> str:
"""
Returns the substring of a string.
Keyword arguments:
str_in -- the string in which to generate a substring from
start -- starting position of the input parameter to start the substring (inclusive)
stop -- stopping position of the input parameter to stop the substring (exclusive)
"""
return str_in[start:stop]
# substring('Congratulations!', 2, 5)
def opposite_case(str_in: str) -> str:
"""
Given a string parameter, this function returns the same string back with each letter having the opposite case.
Example:
When input = "Python" the function returns "pYTHON"
"""
return str_in.swapcase()
# opposite_case('Hello!')
# ballakeerthi@zipcodes-MacBook-Pro-3 PyPart5 % python3 -m unittest test_string_utils.py
# ./Users/ballakeerthi/dev/PyPart5/test_string_utils.py:41: DeprecationWarning: Please use assertEqual instead.
# self.assertEquals(expected, string_utils.input_has_substring(word, substring))
# .....
# ----------------------------------------------------------------------
# Ran 6 tests in 0.001s
#
# OK