Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

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. The test threw an exception when the casing did not match.