Strings are sequence of one or more characters in Python. Strings can be assinged to variables.
1) Immutable 2) Are objects that have methods associated with it.
Let us look at Python string properties through a problem statement.
Test if a given string is a palindrome.
def test_palindrome():
assert is_a_palindrome("ABBA") == True
assert is_a_palindrome("BOB") == True
assert is_a_palindrome("B") == Tru
def test_palindrome():
> assert is_a_palindrome("ABBA") == True
E NameError: name 'is_a_palindrome' is not defined
def is_a_palindrome(term) -> bool:
return False
def test_palindrome():
assert is_a_palindrome("ABBA") == True
assert is_a_palindrome("BOB") == True
assert is_a_palindrome("B") == True
def test_palindrome():
> assert is_a_palindrome("ABBA") == True
E AssertionError: assert False == True
E + where False = is_a_palindrome('ABBA')
strings/palindrome_2.py:6: AssertionError
def is_a_palindrome(term) -> bool:
reversed_term = term[::-1]
if reversed_term == term:
return True
return False
def test_palindrome():
assert is_a_palindrome("ABBA") == True
assert is_a_palindrome("BOB") == True
assert is_a_palindrome("B") == True
assert is_a_palindrome("NOT") == False
strings/palindrome_3.py . [100%]
1 passed in 0.01s
def is_a_palindrome(term) -> bool:
return term == term[::-1]
def test_palindrome():
assert is_a_palindrome("ABBA") == True
assert is_a_palindrome("BOB") == True
assert is_a_palindrome("B") == True
assert is_a_palindrome("NOT") == False