简体   繁体   中英

convert an int to string ascii value

hello I have an int varible in python and would like to get the value of the integer in a string. For example if I have this variable var:int = 0 How to put the value '\0' in a str variable? Thanks for your help.

U can convert most variable types in python by using a simple function: string_variable= str(int)

Hope this helps

Say you have variable var1m that has integer value, to convert it into a string you just need to pass str()

var1 = 1
var2 = '\\' + str(var1)
print(var2)
print(type(var2))

this will output

\1
<class 'str'>

I'm stil not entirely sure what you want but you can try bytes :

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

This will create a byte object from a list of bytes, meaning numbers between 0 and 255. To get a string use str(bytes(...)) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM