Given a string of character the task is to convert each character of a string into the equivalent binary number.
Input : GFG Output : 1000111 1000110 1000111 Input : geeks Output : 1100111 1100101 1100101 1101011 1110011
The idea is to first calculate the length of the string as n and then run a loop n times. In each iteration store ASCII value of character in variable val and then convert it into binary number and store result in array finally print the array in reverse order.
# Python 3 program to convert
# string into binary string
# utility function
bin_conv = 
for c in s:
# convert each char to
# ASCII value
ascii_val = ord(c)
# Convert ASCII value to binary
binary_val = bin(ascii_val)
return (‘ ‘.join(bin_conv))
# Driver Code
if __name__ == ‘__main__’:
s = ‘geeks’
# This code is contributed
# by Vikas Chitturi
1100111 1100101 1100101 1101011 1110011
- Minimum steps to convert one binary string to other only using negation
- Minimum swaps required to convert one binary string to another
- Convert a sentence into its equivalent mobile numeric keypad sequence
- Minimum number of given operations required to convert a string to another string
- Convert the string into palindrome string by changing only one character.
- Convert Hexadecimal value String to ASCII value String
- String with additive sequence
- Convert Binary fraction to Decimal
- Lexicographically largest sub-sequence of the given string
- Convert decimal fraction to binary number
- Convert Ternary Expression to a Binary Tree
- Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array | Set-2
- Check if there exists any sub-sequence in a string which is not palindrome
- Replace every consonant sequence with its length in the given string
- Minimum cuts required to convert a palindromic string to a different palindromic string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.