Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit94549f1

Browse files
committed
Add more string methods.
1 parentcc59b52 commit94549f1

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
asserthello_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+
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+
169201

170202
deftest_string_formatting():
171203
"""String formatting.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp