dawed1999
dawed1999

Reputation: 335

How to add strings to a list?

So i'm trying to add strings of binary to a list but instead, i'm adding each individual character. I'm a new to python so i'm not quite sure how to do this myself(also, when getting the strings I need to ignore the spaces in between each string of binary)

str_msg = "This is a message!"
final = ' '.join(format(ord(x), 'b') for x in str_msg)
print(final)

which gives:

1010100 1101000 1101001 1110011 100000 1101001 1110011 100000 1100001 100000 1101101 1100101 1110011 1110011 1100001 1100111 1100101 100001

but I then try to create the list this way:

binlist = []
for value in final:
    binlist.append(value)
print(binlist)

I get:

['1', '0', '1', '0', '1', '0', '0', ' ', '1', '1', '0', '1', '0', '0', '0', ' ', '1', '1', '0', '1', '0', '0', '1', ' ', '1', '1', '1', '0', '0', '1', '1', ' ', '1', '0', '0', '0', '0', '0', ' ', '1', '1', '0', '1', '0', '0', '1', ' ', '1', '1', '1', '0', '0', '1', '1', ' ', '1', '0', '0', '0', '0', '0', ' ', '1', '1', '0', '0', '0', '0', '1', ' ', '1', '0', '0', '0', '0', '0', ' ', '1', '1', '0', '1', '1', '0', '1', ' ', '1', '1', '0', '0', '1', '0', '1', ' ', '1', '1', '1', '0', '0', '1', '1', ' ', '1', '1', '1', '0', '0', '1', '1', ' ', '1', '1', '0', '0', '0', '0', '1', ' ', '1', '1', '0', '0', '1', '1', '1', ' ', '1', '1', '0', '0', '1', '0', '1', ' ', '1', '0', '0', '0', '0', '1']

Upvotes: 1

Views: 115

Answers (4)

PythonProgrammi
PythonProgrammi

Reputation: 23463

As said above:

str_msg = "This is a message!"
final = ' '.join(format(ord(x), 'b') for x in str_msg)
print(final.split())

output:

['1010100', '1101000', '1101001', '1110011', '100000', '1101001', '1110011', '100000', '1100001', '100000', '1101101', '1100101', '1110011', '1110011', '1100001', '1100111', '1100101', '100001']

list comprehension

str_msg = [format(ord(ch),'b') for ch in "this is a message"]
print(str_msg)

output

['1110100', '1101000', '1101001', '1110011', '100000', '1101001', '1110011', '100000', '1100001', '100000', '1101101', '1100101', '1110011', '1110011', '1100001', '1100111', '1100101']

map

str_msg = list(map(lambda x: format(ord(x), 'b'), 'this is a message'))
print(str_msg)

output

['1110100', '1101000', '1101001', '1110011', '100000', '1101001', '1110011', '100000', '1100001', '100000', '1101101', '1100101', '1110011', '1110011', '1100001', '1100111', '1100101']

Upvotes: 2

dawed1999
dawed1999

Reputation: 335

So while all of the answers i've gotten are vaild, I've choosen to accept @Joe Iddon 's answer since it is the answer that gives me the most optimal code(i.e. the shortest).

Thanks to everybody who helped with the question!

Upvotes: 0

Joe Iddon
Joe Iddon

Reputation: 20434

The expression:

' '.join(format(ord(x), 'b') for x in str_msg)

is using a generator expression to yield each character in the string: str_msg converted to its ASCII value, represented in binary which is then getting passed into the .join method of the string ' ' (a space). As a result, you get a space-separated string of the binary representation of that string.

From the sounds of it, you want a list of strings containing the binary representation of each character.

This can be achieved with a list-comprehension which has the same syntax as a generator expression, but uses square brackets:

[format(ord(x), 'b') for x in str_msg]

Upvotes: 3

Raunaq Jain
Raunaq Jain

Reputation: 917

You just want to split the string by spaces. On your final variable,

'1010100 1101000 1101001 1110011 100000 1101001 1110011 100000 1100001 100000 1101101 1100101 1110011 1110011 1100001 1100111 1100101 100001'

do, final = final.split() and you will get your list

['1010100', '1101000', '1101001', '1110011', '100000', '1101001', '1110011', '100000', '1100001', '100000', '1101101', '1100101', '1110011', '1110011', '1100001', '1100111', '1100101', '100001']

Upvotes: 1

Related Questions