Check if string contains character - Python Last Updated : 01 May, 2025 Comments Improve Suggest changes Like Article Like Report We are given a string and our task is to check if it contains a specific character, this can happen when validating input or searching for a pattern. For example, if we check whether 'e' is in the string 'hello', the output will be True.Using in Operatorin operator is the easiest way to check if a character exists in a string. It returns True if the character is found and False if it is not. Python s = "hello" if "e" in s: print("Character found!") else: print("Character not found!") OutputCharacter found! Explanation: The in operator is used to test membership, returning True if "e" is present in the string, and False otherwise. If found, it prints "Character found!", otherwise, it prints "Character not found!".Using find() Methodfind() method searches for a character in a string and returns its index. If the character is not found, it returns -1. Python s = "hello" if s.find("e") != -1: print("Character found!") else: print("Character not found!") OutputCharacter found! Explanation: The find() method returns the index of the first occurrence of the character, or -1 if it’s not found. If the result is not -1, it prints "Character found!", otherwise, it prints "Character not found!".Using index() Methodindex() method is similar to find(), but it raises an error if the character is not found. So, we need to handle that exception. Python s = "hello" try: s.index("e") print("Character found!") except ValueError: print("Character not found!") OutputCharacter found! Explanation: The index() method raises a ValueError if the character is not found. If no exception is raised, it prints "Character found!"; otherwise, the ValueError is caught by the except block, and it prints "Character not found!".Using count() Methodcount() method counts the number of times a character appears in a string. If the count is greater than 0, the character exists in the string. Python s = "hello" if s.count("e") > 0: print("Character found!") else: print("Character not found!") OutputCharacter found! Explanation: s.count("e") counts how many times "e" appears in s. If the count is greater than 0, it means the character exists in the string hence character found! is printed.Related Articles:Python Membership and Identity OperatorsPython String find() MethodPython String index() MethodPython String count() Method Comment More infoAdvertise with us Next Article Check if string contains character - Python P pragya22r4 Follow Improve Article Tags : Python Python Programs python-string Python string-programs python +1 More Practice Tags : pythonpython Similar Reads Python - Check if String contains any Number We are given a string and our task is to check whether it contains any numeric digits (0-9). For example, consider the following string: s = "Hello123" since it contains digits (1, 2, 3), the output should be True while on the other hand, for s = "HelloWorld" since it has no digits the output should 2 min read Python - Test if String contains any Uppercase character The goal is to check if a given string contains at least one uppercase letter (A-Z). Using any() and isupper()any() function, combined with isdigit(), checks if any character in a string is a digit. It efficiently scans the string and returns True if at least one digit is found.Python# Define the in 3 min read Program to check if a string contains any special character Given a string, the task is to check if that string contains any special character (defined special character set). If any special character is found, don't accept that string. Examples : Input: Geeks$For$GeeksOutput: String is not accepted. Input: Geeks For GeeksOutput: String is accepted Approach 14 min read Check for ASCII String - Python To check if a string contains only ASCII characters, we ensure all characters fall within the ASCII range (0 to 127). This involves comparing each character's value to ensure it meets the criteria.Using str.isascii()The simplest way to do this in Python is by using the built-in str.isascii() method, 2 min read Python | Check if any String is empty in list Sometimes, while working with Python, we can have a problem in which we need to check for perfection of data in list. One of parameter can be that each element in list is non-empty. Let's discuss if a list is perfect on this factor using certain methods. Method #1 : Using any() + len() The combinati 6 min read Python | Check if string is a valid identifier Given a string, write a Python program to check if it is a valid identifier or not. An identifier must begin with either an alphabet or underscore, it can not begin with a digit or any other special character, moreover, digits can come after gfg : valid identifier 123 : invalid identifier _abc12 : v 3 min read Python program to check a string for specific characters Here, will check a string for a specific character using different methods using Python. In the below given example a string 's' and char array 'arr', the task is to write a python program to check string s for characters in char array arr. Examples: Input: s = @geeksforgeeks% arr[] = {'o','e','%'}O 4 min read Python - Test if Kth character is digit in String Given a String, check if Kth index is a digit. Input : test_str = 'geeks9geeks', K = 5 Output : True Explanation : 5th idx element is 9, a digit, hence True.Input : test_str = 'geeks9geeks', K = 4 Output : False Explanation : 4th idx element is s, not a digit, hence False. Method #1: Using in operat 5 min read Test if String Contains Alphabets and Spaces - Python We are given a string and the task is to determine whether it contains only alphabets and spaces, this is often required when working with datasets where the string must not include any digits, punctuation or special characters.Using all() isspace() and isalpha()This method iterates through each cha 3 min read Find position of a character in given string - Python Given a string and a character, our task is to find the first position of the occurrence of the character in the string using Python. For example, consider a string s = "Geeks" and character k = 'e', in the string s, the first occurrence of the character 'e' is at index1. Let's look at various metho 2 min read Like