「なぜこうなるのか?」は、can110さん記載の通りかと思いますので、map と f-stringを使った方法を示します。f-stringsはPython3.6で導入されたものですので、ご注意ください。
Python3.6 から追加された文法機能 - Qiita
コード
python
1text='A'
2for i in map(lambda x:int(x.encode().hex(),16), text):
3 print(f"0x{i:02X} ", end="")
.hex()をしたときに得られる値は'41'という文字列です。
print出力することだけが目的ならば数値への変換(map()の中でやっているint(x,16))が不要かもしれません。
python
1for i in map(lambda x:x.encode().hex(), text):
2 print(f"0x{i} ", end="")
追記:
文字列で扱うならforループいらないですね。
python
1print(*map(lambda x:"0x"+x.encode().hex(), text), sep=" ")
python
1In [8]: text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
2 ...: print(*map(lambda x:"0x"+x.encode().hex(), text), sep=" ")
30x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a
以下は、Jupyter QtConsole 4.3.1の実行結果です。
結果1
python
1In [42]: text='A'
2 ...: for i in map(lambda x:int(x.encode().hex(),16), text):
3 ...: print(f"0x{i:02X} ", end="")
40x41
結果2
textを"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'として実行。
python
1In [48]: text='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
2 ...: for i in map(lambda x:int(x.encode().hex(),16), text):
3 ...: print(f"0x{i:02X} ", end="")
40x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4A 0x4B 0x4C 0x4D 0x4E 0x4F 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5A
結果3
textを'Ascii string ?'として実行。
python
1In [49]: text='Ascii string ?'
2 ...: for i in map(lambda x:int(x.encode().hex(),16), text):
3 ...: print(f"0x{i:02X} ", end="")
40x41 0x73 0x63 0x69 0x69 0x20 0x73 0x74 0x72 0x69 0x6E 0x67 0x20 0x3F
結果4
数値への変換なしのバージョンでtextを'Ascii string ?'として実行。
python
1In [59]: text='Ascii string ?'
2 ...: for i in map(lambda x:x.encode().hex(), text):
3 ...: print(f"0x{i} ", end="")
40x41 0x73 0x63 0x69 0x69 0x20 0x73 0x74 0x72 0x69 0x6e 0x67 0x20 0x3f
2019/01/14 13:21