How to convert string to byte array in Python

3 Answers

0 votes
import binascii

string = "Python"

byte_array = bytearray(string, 'utf-8')

print(binascii.hexlify(byte_array))



'''
run:

b'507974686f6e'

'''

 



answered Mar 10, 2025 by avibootz
0 votes
import binascii

string = "Python"

byte_array = bytes(string, 'utf-8')

print(binascii.hexlify(byte_array))



'''
run:

b'507974686f6e'

'''

 



answered Mar 10, 2025 by avibootz
0 votes
import binascii

string = "Python"

byte_array = string.encode('utf-8')

print(binascii.hexlify(byte_array))



'''
run:

b'507974686f6e'

'''

 



answered Mar 10, 2025 by avibootz

Related questions

1 answer 110 views
1 answer 110 views
110 views asked Jun 7, 2023 by avibootz
1 answer 138 views
2 answers 171 views
171 views asked Apr 30, 2021 by avibootz
3 answers 279 views
279 views asked Apr 30, 2021 by avibootz
...