|
2 | 2 |
|
3 | 3 | @see: https://docs.python.org/3/tutorial/introduction.html
|
4 | 4 | @see: https://www.w3schools.com/python/python_strings.asp
|
| 5 | +@see: https://www.w3schools.com/python/python_ref_string.asp |
5 | 6 |
|
6 | 7 | Besides numbers, Python can also manipulate strings, which can be
|
7 | 8 | expressed in several ways. They can be enclosed in single quotes ('...')
|
@@ -166,6 +167,37 @@ def test_string_methods():
|
166 | 167 | # The split() method splits the string into substrings if it finds instances of the separator.
|
167 | 168 | asserthello_world_string.split(',')== ['Hello',' World!']
|
168 | 169 |
|
| 170 | +# Converts the first character to upper case |
| 171 | +assert'low letter at the beginning'.capitalize()=='Low letter at the beginning' |
| 172 | + |
| 173 | +# Returns the number of times a specified value occurs in a string. |
| 174 | +assert'low letter at the beginning'.count('t')==4 |
| 175 | + |
| 176 | +# Searches the string for a specified value and returns the position of where it was found. |
| 177 | +assert'Hello, welcome to my world'.find('welcome')==7 |
| 178 | + |
| 179 | +# Converts the first character of each word to upper case |
| 180 | +assert'Welcome to my world'.title()=='Welcome To My World' |
| 181 | + |
| 182 | +# Returns a string where a specified value is replaced with a specified value. |
| 183 | +assert'I like bananas'.replace('bananas','apples')=='I like apples' |
| 184 | + |
| 185 | +# Joins the elements of an iterable to the end of the string. |
| 186 | +my_tuple= ('John','Peter','Vicky') |
| 187 | +assert', '.join(my_tuple)=='John, Peter, Vicky' |
| 188 | + |
| 189 | +# Returns True if all characters in the string are upper case. |
| 190 | +assert'ABC'.isupper() |
| 191 | +assertnot'AbC'.isupper() |
| 192 | + |
| 193 | +# Check if all the characters in the text are letters. |
| 194 | +assert'CompanyX'.isalpha() |
| 195 | +assertnot'Company 23'.isalpha() |
| 196 | + |
| 197 | +# Returns True if all characters in the string are decimals. |
| 198 | +assert'1234'.isdecimal() |
| 199 | +assertnot'a21453'.isdecimal() |
| 200 | + |
169 | 201 |
|
170 | 202 | deftest_string_formatting():
|
171 | 203 | """String formatting.
|
|