Python check character in string

Check if String Contains Substring in Python

Checking if a string contains a substring is one of the most common tasks in any programming language. Python offers many ways to check if a string contains a substring. The simplest and fastest way to check whether a string contains a substring or not in Python is using the "in" operator , which is used as a comparison operator . Some other Python methods such as find[], index[], count[] etc. also help to Check if a string contains a substring.

Using Python's "in" operator

The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .

output

Python "in" operator takes two arguments, one on the left and one on the right, and returns True if the left argument string is contained within the right argument string.

More on "in" operator

Note: The "in" operator is case sensitive i.e, it will treat the Uppercase characters and Lowercase characters differently.

__contains__[] function

Python String class has the __contains__[] method that we can use to check if it contains another string or not. When we use Python "in" operator , internally it calls __contains__[] function. The __contains__ method defines how instances of class behave when they appear at right side of in and not in operator. We can use this function directly too, but don't . Methods that start with underscores are considered semantically private , however it's recommended to use in operator for readability purposes.

Using Python's str.find[] method

Another method you can use is the string.find[] method . The find[] method evaluate if the string contains a substring . If it does, the find[] method returns the starting index of a substring within the string otherwise it returns -1 .

output

More on find[] method

Using str.find[] method is the less Pythonic way , but it's still accepted. It's longer and a little bit more confusing, but it still gets the job done.

Using Python Regular Expression

Regular expression is widely used for pattern matching . Python has a built-in package called re , which can be used to work with Regular Expressions . The re module contains a function called search[] , it can be used to check if a string contains the specified search pattern .

example

output

Using str.count[] method

If you want to count the number of occurrences of a specific substring in a string, then you can use Python count[] method . If the substring is not found in a string, the function returns 0 .


How can I check if a string has several specific characters in it using Python 2?

For example, given the following string:

The criminals stole $1,000,000 in jewels.

How do I detect if it has dollar signs ["$"], commas [","], and numbers?

Jesuisme

1,6351 gold badge31 silver badges39 bronze badges

asked Mar 4, 2011 at 1:47

3

Assuming your string is s:

'$' in s        # found
'$' not in s    # not found

# original answer given, but less Pythonic than the above...
s.find['$']==-1 # not found
s.find['$']!=-1 # found

And so on for other characters.

... or

pattern = re.compile[r'\d\$,']
if pattern.findall[s]:
    print['Found']
else
    print['Not found']

... or

chars = set['0123456789$,']
if any[[c in chars] for c in s]:
    print['Found']
else:
    print['Not Found']

[Edit: added the '$' in s answers]

answered Mar 4, 2011 at 2:07

dappawitdappawit

11.7k2 gold badges31 silver badges26 bronze badges

7

user Jochen Ritzel said this in a comment to an answer to this question from user dappawit. It should work:

['1' in var] and ['2' in var] and ['3' in var] ...

'1', '2', etc. should be replaced with the characters you are looking for.

See this page in the Python 2.7 documentation for some information on strings, including about using the in operator for substring tests.

Update: This does the same job as my above suggestion with less repetition:

# When looking for single characters, this checks for any of the characters...
# ...since strings are collections of characters
any[i in '' for i in '123']
# any[i in 'a' for i in '123'] -> False
# any[i in 'b3' for i in '123'] -> True

# And when looking for subsrings
any[i in '' for i in ['11','22','33']]
# any[i in 'hello' for i in ['18','36','613']] -> False
# any[i in '613 mitzvahs' for i in ['18','36','613']] ->True

answered Mar 4, 2011 at 2:39

AbbafeiAbbafei

3,0183 gold badges26 silver badges24 bronze badges

2

Quick comparison of timings in response to the post by Abbafei:

import timeit

def func1[]:
    phrase = 'Lucky Dog'
    return any[i in 'LD' for i in phrase]

def func2[]:
    phrase = 'Lucky Dog'
    if ['L' in phrase] or ['D' in phrase]:
        return True
    else:
        return False

if __name__ == '__main__': 
    func1_time = timeit.timeit[func1, number=100000]
    func2_time = timeit.timeit[func2, number=100000]
    print['Func1 Time: {0}\nFunc2 Time: {1}'.format[func1_time, func2_time]]

Output:

Func1 Time: 0.0737484362111
Func2 Time: 0.0125144964371

So the code is more compact with any, but faster with the conditional.

EDIT : TL;DR -- For long strings, if-then is still much faster than any!

I decided to compare the timing for a long random string based on some of the valid points raised in the comments:

# Tested in Python 2.7.14

import timeit
from string import ascii_letters
from random import choice

def create_random_string[length=1000]:
    random_list = [choice[ascii_letters] for x in range[length]]
    return ''.join[random_list]

def function_using_any[phrase]:
    return any[i in 'LD' for i in phrase]

def function_using_if_then[phrase]:
    if ['L' in phrase] or ['D' in phrase]:
        return True
    else:
        return False

if __name__ == '__main__':
    random_string = create_random_string[length=2000]
    func1_time = timeit.timeit[stmt="function_using_any[random_string]",
                               setup="from __main__ import function_using_any, random_string",
                               number=200000]
    func2_time = timeit.timeit[stmt="function_using_if_then[random_string]",
                               setup="from __main__ import function_using_if_then, random_string",
                               number=200000]
    print['Time for function using any: {0}\nTime for function using if-then: {1}'.format[func1_time, func2_time]]

Output:

Time for function using any: 0.1342546
Time for function using if-then: 0.0201827

If-then is almost an order of magnitude faster than any!

answered Jul 14, 2015 at 17:11

JesuismeJesuisme

1,6351 gold badge31 silver badges39 bronze badges

3

This will test if strings are made up of some combination or digits, the dollar sign, and a commas. Is that what you're looking for?

import re

s1 = 'Testing string'
s2 = '1234,12345$'

regex = re.compile['[0-9,$]+$']

if [ regex.match[s1] ]:
   print "s1 matched"
else:
   print "s1 didn't match"

if [ regex.match[s2] ]:
   print "s2 matched"
else:
   print "s2 didn't match"

answered Mar 4, 2011 at 2:01

ajwoodajwood

17.1k15 gold badges58 silver badges100 bronze badges

3

Check if chars are in String:

parse_string = lambda chars, string: [char in string for char in chars]

example:

parse_string['$,x', 'The criminals stole $1,000,000 in ....'] 

or

parse_string[['$', ',', 'x'], '..minals stole $1,000,000 i..']

output: [True, True, False]

answered Mar 16, 2021 at 18:19

FelixFelix

112 bronze badges

My simple, simple, simple approach! =D

Code

string_to_test = "The criminals stole $1,000,000 in jewels."
chars_to_check = ["$", ",", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
for char in chars_to_check:
    if char in string_to_test:
        print["Char \"" + char + "\" detected!"]

Output

Char "$" detected!
Char "," detected!
Char "0" detected!
Char "1" detected!

bad_coder

9,18619 gold badges37 silver badges61 bronze badges

answered Dec 29, 2020 at 15:27

Eduardo LucioEduardo Lucio

1,2551 gold badge19 silver badges33 bronze badges

s=input["Enter any character:"]   
if s.isalnum[]:   
   print["Alpha Numeric Character"]   
   if s.isalpha[]:   
       print["Alphabet character"]   
       if s.islower[]:   
         print["Lower case alphabet character"]   
       else:   
         print["Upper case alphabet character"]   
   else:   
     print["it is a digit"]   
elif s.isspace[]:   
    print["It is space character"]   

else:
print["Non Space Special Character"]

answered Aug 21, 2018 at 15:36

NagarajNagaraj

5682 gold badges5 silver badges8 bronze badges

2

Not the answer you're looking for? Browse other questions tagged python string or ask your own question.

How do you check if a character is in a string?

The contains[] method checks whether a string contains a sequence of characters. Returns true if the characters exist and false if not.

How do you check if a word is in a string Python?

The simplest way to check if a string contains a substring in Python is to use the in operator. This will return True or False depending on whether the substring is found. For example: sentence = 'There are more trees on Earth than stars in the Milky Way galaxy' word = 'galaxy' if word in sentence: print['Word found.

Chủ Đề