Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94549f1

Browse files
committed
Add more string methods.
1 parent cc59b52 commit 94549f1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎src/data_types/test_strings.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
@see: https://docs.python.org/3/tutorial/introduction.html
44
@see: https://www.w3schools.com/python/python_strings.asp
5+
@see: https://www.w3schools.com/python/python_ref_string.asp
56
67
Besides numbers, Python can also manipulate strings, which can be
78
expressed in several ways. They can be enclosed in single quotes ('...')
@@ -166,6 +167,37 @@ def test_string_methods():
166167
# The split() method splits the string into substrings if it finds instances of the separator.
167168
assert hello_world_string.split(',') == ['Hello', ' World!']
168169

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+
assert not 'AbC'.isupper()
192+
193+
# Check if all the characters in the text are letters.
194+
assert 'CompanyX'.isalpha()
195+
assert not 'Company 23'.isalpha()
196+
197+
# Returns True if all characters in the string are decimals.
198+
assert '1234'.isdecimal()
199+
assert not 'a21453'.isdecimal()
200+
169201

170202
def test_string_formatting():
171203
"""String formatting.

0 commit comments

Comments
 (0)