Search notes:

Python (membership-) operator in

Testing for a substring

x in txt can be used to test if a string (x) is a substring of another string (txt).
The test is performed case-sensitively ('BAR' in 'foo bar baz' returns False).
#!/usr/bin/python3

def is_substring_contained_in_string(substring, string):
    if substring in string:
       print(substring + ' is a substring of ' + string)
    else:
       print(substring + ' is not a substring of ' + string)

is_substring_contained_in_string('foo', 'hello world')
# foo is not a substring of hello world

is_substring_contained_in_string('bar', 'foo bar baz')
# bar is a substring of foo bar baz

is_substring_contained_in_string('in' , 'wintertime' )
# in is a substring of wintertime

is_substring_contained_in_string('BAR' , 'foo bar baz' )
# BAR is not a substring of foo bar baz
Github repository about-python, path: /operators/in/substr-in-str.py

See also

operators

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758199200, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/operators/in/index(62): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78