Often I check if a substring is present in a string. In Python I could do that either using the __contains__()
method or using the in
operator as shown below:
Pattern 1:
string1 = r'c:\users\username\documents\code\crime_data.csv'
print(string1.__contains__('username'))
Pattern 2:
print('username' in string1)
Both work well for me, but I would like to know why is __contains__
hidden method and if pattern 2 preferred over 1, if so why? Other programming languages like C# do have a String.contains()
method in their standard library.