Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python check if string in string

if "blah" not in somestring: 
    continue
Comment

How to check for string membership in python

s1 = 'perpendicular'
s2 = 'pen'
s3 = 'pep'

print(''pen' in 'perpendicular' -> {}'.format(s2 in s1))
print(''pep' in 'perpendicular' -> {}'.format(s3 in s1))


# Output

# 'pen' in 'perpendicular' -> True
# 'pep' in 'perpendicular' -> False


# If you are more interested in finding the location of a substring within a string
# (as opposed to simply checking whether or not the substring is contained),
# the find() string method can be more helpful.

s = 'Does this string contain a substring?'

print(''string' location -> {}'.format(s.find('string')))
print(''spring' location -> {}'.format(s.find('spring')))


# Output

# 'string' location -> 10
# 'spring' location -> -1


# find() returns the index of the first character of the first occurrence of the substring by default,
# and returns -1 if the substring is not found
Comment

PREVIOUS NEXT
Code Example
Python :: lenet 5 keras 
Python :: #index operator in python 
Python :: Pass arguments in button tkinter 
Python :: json payload python function 
Python :: numpy cumsum 
Python :: how to run python in the browser 
Python :: application automation python library 
Python :: python dictionary print key value ascending order 
Python :: change increment in for loop python 
Python :: time zone 
Python :: filter in python 
Python :: Python Pandas - How to write in a specific column in an Excel Sheet 
Python :: alternative to time.sleep() in python 
Python :: next day in python 
Python :: python first 
Python :: librosa python 
Python :: dictionary from two list 
Python :: NumPy flipud Example 
Python :: python dictionary add item 
Python :: extract directory python 
Python :: implement stack using list in python 
Python :: .translate python 
Python :: how to check how many key value pairs are in a dict python 
Python :: split strings around given separator/delimiter 
Python :: insert blank row in data frame 
Python :: df loc 
Python :: negative slicing in python list 
Python :: Function to plot as many bars as you wish 
Python :: Random Colored Shapes with python turtle 
Python :: Math Module cos() Function in python 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =