
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to check if a string contains any unique character
In this tutorial, we are going to write a program that checks whether a string contains any special character or not. It's straightforward in Python.
We will have a set of special characters in the string module. We can use that one to check whether a string contains any special characters or not. Let's see the steps to write the program.
Import the string module.
Store the special characters from string.punctuation in a variable.
Initialize a string.
Check whether the string has special characters or not using the map function.
Print the result, whether valid or not.
Example
# importing the string module import string # special characters special_chars = string.punctuation # initializing a string string_1 = "Tutori@lspoinT!" string_2 = "Tutorialspoint" # checking the special chars in the string_1 bools = list(map(lambda char: char in special_chars, string_1)) print("Valid") if any(bools) else print("Invalid") # checking the special chars in the string_2 bools = list(map(lambda char: char in special_chars, string_2)) print("Valid") if any(bools) else print("Invalid")
Output
If you run the above program you will get the following result.
Valid Invalid
Conclusion
You can move the code to a function to avoid the redundancy in the code. If you have any doubts in the tutorial, mention them in the comment section.
- Related Articles
- Program to check if a string contains any special character in Python
- C# program to check if a string contains any special character
- Java program to check if a string contains any special character
- Program to check if a string contains any special character in C
- Python program to check if a string contains all unique characters
- Java Program to check if the String contains any character in the given set of characters
- Check if list contains all unique elements in Python
- Java Program to Check if a string contains a substring
- Golang program to check if a string contains a substring
- Python Program to check if String contains only Defined Characters using Regex
- How to check if a Python string contains only digits?
- PHP program to check if a string has a special character
- How to check if a string contains only one type of character in R?
- First Unique Character in a String in Python
- How to check if a given directory contains any other directory in Python?
