Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,652 questions

51,529 answers

573 users

How to convert text to binary code in Python

1 Answer

0 votes
def text2bin(txt):
    bin_result = ""

    for char in txt:
        # Convert character to ASCII and then to binary
        binary = bin(ord(char))[2:]  # Remove the '0b' prefix
        # Pad binary to ensure it is 8 bits long
        binary = binary.zfill(8)
        # Append to the result with a space
        bin_result += binary + " "

    return bin_result.strip()  # Remove trailing space

print(text2bin("Python"))



'''
run:

01010000 01111001 01110100 01101000 01101111 01101110

'''

 



answered Apr 13, 2025 by avibootz
...