Python - Check if a given string is binary string or not (2024)

In this article we check if a given string has characters which are only 1 or 0. We call such strings as binary strings. If it has any other digit like 2 or 3 etc., we classify it as a non-binary string.

With set

The set operator in python stores only unique elements. So we take a string and apply the set function to it. Then we create another set which has only 0 and 1 as its elements. If both these sets are equal then the string is definitely binary. Also the string may have only 1s or only 0s. So we create a or condition which will also compare the result of set operator only with 0 or only with 1.

Example

Live Demo

stringA = '0110101010111'b = {'0','1'}t = set(stringA)if b == t or t == {'0'} or t == {'1'}:print("StringA is a binary string.")else:print("StringA is not a binary string.")stringB = '0120101010111'u = set(stringB)if b == u or u == {'0'} or u == {'1'}:print("StringB is a binary string.")else:print("StringB is not a binary string.")

Output

Running the above code gives us the following result −

StringA is a binary string.StringB is not a binary string.

With simple iteration

We can first declare a string with value as 01 or 10. Then compare the chraracters of this string with the characters of the given string. If the

Example

Live Demo

stringA = "01100000001"b = '10'count = 0for char in stringA: if char not in b: count = 1 break else: passif count: print("StringA is not a binary string")else: print("StringA is a binary string")stringB = "01200000001"for char in stringB: if char not in b: count = 1 break else: passif count: print("StringB is not a binary string")else: print("StringB is a binary string")

Output

Running the above code gives us the following result −

StringA is a binary stringStringB is not a binary string
Kickstart Your Career

Get certified by completing the course

Get Started

Python - Check if a given string is binary string or not (2)

Advertisem*nts

Python - Check if a given string is binary string or not (2024)
Top Articles
What Is SEER? (2024 Guide)
Guarda Wallet Review: Best Light Wallet?
It's Official: Sabrina Carpenter's Bangs Are Taking Over TikTok
Bj 사슴이 분수
Boggle Brain Busters Bonus Answers
Wausau Marketplace
Apply A Mudpack Crossword
THE 10 BEST River Retreats for 2024/2025
Ecers-3 Cheat Sheet Free
Bill Devane Obituary
Becky Hudson Free
Gt Transfer Equivalency
The fabulous trio of the Miller sisters
Price Of Gas At Sam's
Define Percosivism
Elemental Showtimes Near Cinemark Flint West 14
3S Bivy Cover 2D Gen
Daylight Matt And Kim Lyrics
/Www.usps.com/International/Passports.htm
Crawlers List Chicago
Pokemon Unbound Shiny Stone Location
Tips on How to Make Dutch Friends & Cultural Norms
Sodium azide 1% in aqueous solution
Jail View Sumter
Asteroid City Showtimes Near Violet Crown Charlottesville
Mythical Escapee Of Crete
Sherburne Refuge Bulldogs
Craigslist Rentals Coquille Oregon
27 Fantastic Things to do in Lynchburg, Virginia - Happy To Be Virginia
Publix Christmas Dinner 2022
Craigs List Jax Fl
Tokioof
Life Insurance Policies | New York Life
Log in or sign up to view
Craigslist West Seneca
What Are Digital Kitchens & How Can They Work for Foodservice
Ket2 Schedule
Domino's Delivery Pizza
Chs.mywork
Baywatch 2017 123Movies
Midsouthshooters Supply
Robeson County Mugshots 2022
Raising Canes Franchise Cost
Coroner Photos Timothy Treadwell
The power of the NFL, its data, and the shift to CTV
Advance Auto.parts Near Me
Gt500 Forums
Shiftselect Carolinas
7 Sites to Identify the Owner of a Phone Number
Scholar Dollar Nmsu
Www.card-Data.com/Comerica Prepaid Balance
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 5667

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.