<p>To add to Andrew's answer I recently used the above code snippet to generate bech32m addresses from P2TR scriptPubKeys.</p>
<p>I cloned the bech32 <a href="https://github.com/sipa/bech32" rel="nofollow noreferrer">repo</a> and then created a file for the code snippet in the <code>bech/ref/python</code> directory.</p>
<p>I took a P2TR scriptPubKey from the BIP 341 <a href="https://github.com/bitcoin/bips/blob/master/bip-0341/wallet-test-vectors.json" rel="nofollow noreferrer">test vectors</a> e.g. <code>5120712447206d7a5238acc7ff53fbe94a3b64539ad291c7cdbc490b7577e4b17df5</code></p>
<p>Then this slightly adjusted code generated the associated bech32m address (<code>bc1pwyjywgrd0ffr3tx8laflh6228dj98xkjj8rum0zfpd6h0e930h6saqxrrm</code>).</p>
<pre><code>import segwit_addr
import binascii

spk = binascii.unhexlify('5120712447206d7a5238acc7ff53fbe94a3b64539ad291c7cdbc490b7577e4b17df5')
version = spk[0] - 0x50 if spk[0] else 0
program = spk[2:]
print(segwit_addr.encode('bc', version, program))
</code></pre>

CC BY-SA 4.0
2022-06-09T11:37:22.547Z
2022-06-09T11:37:22.547Z
Michael Folkson
<p>The reference implementation provides a handy all-in-one encode function. To encode the scriptPubKey <code>0014751e76e8199196d454941c45d1b3a323f1433bd6</code> as a mainnet bech32 address, you would do:</p>

<pre><code>import bech32
import binascii

spk = binascii.unhexlify('0014751e76e8199196d454941c45d1b3a323f1433bd6')
version = spk[0] - 0x50 if spk[0] else 0
program = spk[2:]
print(bech32.encode('bc', version, program))
</code></pre>

CC BY-SA 4.0
2019-11-18T17:49:35.870Z
2019-11-18T17:49:35.870Z
Ava Chow
<p>Could someone provide an example of how to use the <a href="https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py" rel="nofollow noreferrer">python reference implementation</a> for generating a bech32 address?</p>

<p>For example, generating a mainnet bech32 address for this scriptPubKey: "0014751e76e8199196d454941c45d1b3a323f1433bd6"</p>

<python><bech32-address><bip350-bech32m>
How to use python reference for encoding a bech32 address?
Darius
# How to use python reference for encoding a bech32 address?

(Asked by: username_0 on 2019-11-18)

Could someone provide an example of how to use the [python reference implementation](https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py) for generating a bech32 address?

For example, generating a mainnet bech32 address for this scriptPubKey: "0014751e76e8199196d454941c45d1b3a323f1433bd6"

**Tags:** python, bech32-address, bip350-bech32m


---

## 2 answers

### Answer by username_2 on 2019-11-18. Score: 5 | Accepted answer

The reference implementation provides a handy all-in-one encode function. To encode the scriptPubKey `0014751e76e8199196d454941c45d1b3a323f1433bd6` as a mainnet bech32 address, you would do:

```
import bech32
import binascii

spk = binascii.unhexlify('0014751e76e8199196d454941c45d1b3a323f1433bd6')
version = spk[0] - 0x50 if spk[0] else 0
program = spk[2:]
print(bech32.encode('bc', version, program))

```


---

### Answer by username_1 on 2022-06-09. Score: 1

To add to Andrew's answer I recently used the above code snippet to generate bech32m addresses from P2TR scriptPubKeys.

I cloned the bech32 [repo](https://github.com/sipa/bech32) and then created a file for the code snippet in the `bech/ref/python` directory.

I took a P2TR scriptPubKey from the BIP 341 [test vectors](https://github.com/bitcoin/bips/blob/master/bip-0341/wallet-test-vectors.json) e.g. `5120712447206d7a5238acc7ff53fbe94a3b64539ad291c7cdbc490b7577e4b17df5`

Then this slightly adjusted code generated the associated bech32m address (`bc1pwyjywgrd0ffr3tx8laflh6228dj98xkjj8rum0zfpd6h0e930h6saqxrrm`).

```
import segwit_addr
import binascii

spk = binascii.unhexlify('5120712447206d7a5238acc7ff53fbe94a3b64539ad291c7cdbc490b7577e4b17df5')
version = spk[0] - 0x50 if spk[0] else 0
program = spk[2:]
print(segwit_addr.encode('bc', version, program))

```


---

