Time for action – comparing strings

Let's compare strings. Both strings are the word "NumPy":

  1. Call the assert_string_equal() function to compare a string with itself. This test, of course, should pass:
    print("Pass", np.testing.assert_string_equal("NumPy", "NumPy"))

    The test passes:

    Pass None
    
  2. Call the assert_string_equal() function to compare a string with another string with the same letters, but different casing. This test should throw an exception:
    print("Fail", np.testing.assert_string_equal("NumPy", "Numpy"))

    The test raises an error:

    Fail
    Traceback (most recent call last):
    
        raise AssertionError(msg)
    AssertionError: Differences in strings:
    - NumPy?    ^
    + Numpy?    ^
    

What just happened?

We compared two strings with the assert_string_equal() function. ...

Get NumPy : Beginner's Guide - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.