Change language

Python | Check if a given string is a binary string or not

| |

Examples :

 Input: str = "01010101010" Output: Yes Input: str = "geeks101" Output: No 

Approach 1 : Using

# Python program to check
# if the string is binary or not

 
# function to check
# line is accepted or not

def check (string):

 

  # install string transform function

< code class = "undefined spaces">  # to the character set.

p = set (string)

 

# declare a set of & # 39; 0 & # 39 ;, & # 39; 1 & # 39 ;.

s = { ’0’ , ’1’ }

 

# check that the p set is the same as set s

# or set p contains only & # 39; 0 & # 39;

  # or set p contains only & # 39; 1 & # 39;

  # or not, if there is one condition

# true, then the line was accepted

  # not otherwise.

if s = = p or p = = { ’0’ } or p = = { ’ 1’  }:

print ( "Yes" )

else :

print ( "No" )

 

 

 
# driver code

if __ name__ = = "__ main __" :

 

string = " 101010000111 "

  

# function call

check (string)

Output:

 Yes 

Approach 2: simple

# Python program to check
# if the string is binary or not

 
# function to check
# line was accepted or not

def check2 (string):

 

# initialize variable t

# with the line & # 39; 01 & # 39;

t = ’01’

  

# initialize the count variable

  # with a value of 0

  count = 0

  

  # loop through each character

# lines.

for char in string:

 

# check if character is present in

# string t or not.

# if this condition is true

# assign 1 to count variable

  # and break out of the for loop

# otherwise pass

if char not in t:

count = 1

  break

else :

pass

  

# after exiting the loop

# check the counter value is not zero or not

# if the value nonzero, en condition is true

# and the string is not accepted

# otherwise the line is accepted

if count:

print ( "No" )

else :

print ( " Yes " )

  

 

 
# driver code

  if __ name__ = = "__ main__" :

 

string = "001021010001010"

 

  # function call

check2 (string)

Exit :

 No 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically