• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / Python / Check if String Contains Only Certain Characters in Python

Check if String Contains Only Certain Characters in Python

February 11, 2022 Leave a Comment

In Python, we can easily check if a string contains certain characters using a for loop and check individually if each character is one of those characters or not.

def containsCertainChars(string, chars):
    for char in string:
        if char in chars:
           return True
    return False

print(containsCertainChars("Hello World!", "H"))
print(containsCertainChars("Hello World!", "olz"))
print(containsCertainChars("Hello World!", "z"))

#Output:
True
True
False

When working with strings, it can be useful to know if there are certain characters contained in a string variable.

In Python, we can easily get if a string contains certain characters in a string by looping over each character of the string and seeing if it is one of the given characters or not.

Below is a function which will check if a string has certain characters or not for you in a string using Python.

def containsCertainChars(string, chars):
    for char in string:
        if char in chars:
           return True
    return False

print(containsCertainChars("Hello World!", "H"))
print(containsCertainChars("Hello World!", "olz"))
print(containsCertainChars("Hello World!", "z"))

#Output:
True
True
False

Checking if a Certain Character Appears in a String Using Python

The example above is useful if you want to check if any character in your string or list is in a string. We can also use Python to check if each of the characters appear in a string.

To do this, we will loop over the character and create a dictionary which stores if we find each of the character.

Below is a Python function which will check if a string has a particular character.

def checkEachChar(string, chars):
    checks = {}
    for char in chars:
        checks[char] = string.count(char) > 0
    return checks

print(checkEachChar("Hello World!", "Hl"))
print(checkEachChar("This is a string with some words.", "SwDds"))
print(checkEachChar("What's up?", "Uu"))
print(checkEachChar("Brrrr", "Pr"))

#Output:
{'H': True, 'l': True}
{'S': False, 'w': True, 'D': False, 'd': True, 's': True}
{'U': False, 'u': True}
{'P': False, 'r': True}

Checking if a Vowel Appears in a String Using Python

When working with strings, it can be useful to know if there are any vowels contained in a string variable.

In Python, we can easily check if a string contains vowels in a string looping over each character of the string and seeing if it is a vowel or not.

We can take the functions from above and modify them slightly to see if there are any vowels in a string.

Below is a Python function which will check if a string has a specific vowel.

def checkEachVowel(string):
    checks = {}
    string = string.lower()
    for vowel in "aeiou":
        checks[vowel] = string.count(vowel) > 0
    return checks

print(checkEachVowel("Hello World!"))
print(checkEachVowel("This is a string with some words."))
print(checkEachVowel("What's up?"))
print(checkEachVowel("Brrrr"))

#Output:
{'a': False, 'e': True, 'i': False, 'o': True, 'u': False}
{'a': True, 'e': True, 'i': True, 'o': True, 'u': False}
{'a': True, 'e': False, 'i': False, 'o': False, 'u': True}
{'a': False, 'e': False, 'i': False, 'o': False, 'u': False}

Hopefully this article has been useful for you to check if a string contains certain characters using Python.

Other Articles You'll Also Like:

  • 1.  PROC PHREG Equivalent in Python
  • 2.  How to Check if a Dictionary is Empty in Python
  • 3.  Zip Two Lists in Python
  • 4.  Inverting Dictionary Variables in Python with Dictionary Comprehension
  • 5.  Python cube root – Find Cube Root of Number With math.pow() Function
  • 6.  Using Python to Find Second Largest Value in List
  • 7.  What is the Correct File Extension for Python Files?
  • 8.  Using Python to Read Random Line from File
  • 9.  Print Time Elapsed in Python
  • 10.  Python atan – Find Arctangent and Inverse Tangent of Number

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy