# How to convert a string to hex in Python

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about How to convert a string to hex in Python. so without wasting time lets learn about of this.

## How to convert a string to hex in Python

1. How to convert a string to hex in Python

to convert string to hex in python use this steps. Convert string to integer:
By using int(str,16) you can convert string to integer in python. Lets learn about of this by given below example: `mystr = "0xDC" print(int(mystr, 16))` Output : `220`
Convert integer to hex value:
By using hex() you can convert integer to hex in python. Lets learn about of this by given below example: `print(hex(220))` Output :
`0xdc`

2. string to hex in python

to convert string to hex in python use this steps. Convert string to integer:
By using int(str,16) you can convert string to integer in python. Lets learn about of this by given below example: `mystr = "0xDC" print(int(mystr, 16))` Output : `220`
Convert integer to hex value:
By using hex() you can convert integer to hex in python. Lets learn about of this by given below example: `print(hex(220))` Output :
`0xdc`

3. str to hex python

to convert string to hex in python use this steps. Convert string to integer:
By using int(str,16) you can convert string to integer in python. Lets learn about of this by given below example: `mystr = "0xDC" print(int(mystr, 16))` Output : `220`
Convert integer to hex value:
By using hex() you can convert integer to hex in python. Lets learn about of this by given below example: `print(hex(220))` Output :
`0xdc`

## Method 1: Use int(str,16)

### Convert string to integer:

By using int(str,16) you can convert string to integer in python. Lets learn about of this by given below example:

``````mystr = "0xDC"
print(int(mystr, 16))``````

Output :

``220``

### Convert integer to hex value:

By using hex() you can convert integer to hex in python. Lets learn about of this by given below example:

``````print(hex(220))
``````

Output :

``0xdc``

## Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?