Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将整型转换为字符串ascii值_Python_Python 3.x - Fatal编程技术网

Python 将整型转换为字符串ascii值

Python 将整型转换为字符串ascii值,python,python-3.x,Python,Python 3.x,您好,我在python中有一个int变量,希望获取字符串中整数的值。 例如,如果我有这个变量 var:int=0 如何将值“\0”放入str变量中? 感谢您的帮助。您可以使用一个简单的函数转换python中的大多数变量类型: 字符串_变量=str(int) 希望这有助于假设变量var1m具有整数值,要将其转换为字符串,只需传递str() 这将输出 \1 <class 'str'> \1 我仍然不能完全确定您想要什么,但您可以尝试字节: >>> bytes([0])

您好,我在python中有一个int变量,希望获取字符串中整数的值。 例如,如果我有这个变量 var:int=0 如何将值“\0”放入str变量中?
感谢您的帮助。

您可以使用一个简单的函数转换python中的大多数变量类型: 字符串_变量=str(int)


希望这有助于

假设变量var1m具有整数值,要将其转换为字符串,只需传递str()

这将输出

\1
<class 'str'>
\1

我仍然不能完全确定您想要什么,但您可以尝试
字节

>>> bytes([0])
b'\x00'
>>> bytes([1])
b'\x01'
>>> bytes([0, 1])
b'\x00\x01'

这将从字节列表中创建字节对象,表示0到255之间的数字。要获取字符串,请使用
str(字节(…)

我将获取字符“0”,而不是“\0”。为什么要“\0”?例如,如果int是1或2,您想要什么?它应该有一个前导反斜杠的期望是什么?该反斜杠是否应仅应用于每个int,而不管其值如何?如果int为0,则需要“\0”值,如果值为1,则需要“\x01”。
>>> bytes([0])
b'\x00'
>>> bytes([1])
b'\x01'
>>> bytes([0, 1])
b'\x00\x01'