Python | Check if a given string is binary string or not
Given a string str. The task is to check whether it is a binary string or not.
Input: str = "01010101010" Output: Yes Input: str = "geeks101" Output: No
Approach 1: Using Set
- Insert the given string in a set
- Check if the set characters consist of 1 and/or 0 only.
Approach 2: Simple Iteration
- Iterate for every character and check if the character is 0 or 1.
- If it is not then set a counter and break.
- After iteration check whether the counter is set or not.
Approach 3: Regular Expressions
- Compile a regular expression using compile() for “character is not 0 or 1”.
- Use re.findall() to fetch the strings satisfying the above regular expression.
- Print output based on the result.