<p>Check out <a href="https://github.com/cffls/pycardano" rel="nofollow noreferrer">pycardano</a>.
Class <code>Address</code> has a class method <code>from_primitive</code> that can read and decode a bech32 address. Then you can directly read field <code>payment_part</code>, which is the public key hash of the payment credential from the address object. You can find the API documentation <a href="https://pycardano.readthedocs.io/en/latest/api/pycardano.address.html#pycardano.address.Address" rel="nofollow noreferrer">here</a>.</p>
<p>Example:</p>
<pre><code>&gt;&gt;&gt; from pycardano import Address   
&gt;&gt;&gt; addr = Address.from_primitive(&quot;addr_test1vzv89dr7v69enywx0t7sqcyxshcmnlv9q43lhpg7ecfhjjsjcdsza&quot;)
&gt;&gt;&gt; addr.payment_part
VerificationKeyHash(hex='9872b47e668b9991c67afd00608685f1b9fd850563fb851ece13794a')
</code></pre>

CC BY-SA 4.0
2022-04-26T04:16:20.220Z
2022-04-26T04:16:20.220Z
Jerry
<p>I know that this is possible in javascript using the cardano serialization lib, as follows:</p>
<pre><code>function toHex(bytes){
    return Buffer.from(bytes).toString(&quot;hex&quot;);
}

function addrToPubKeyHash(bech32Addr) {
  const pkh = Loader.Cardano.BaseAddress.from_address(
    Loader.Cardano.Address.from_bech32(bech32Addr)
  )
    .payment_cred()
    .to_keyhash();

  return toHex(pkh.to_bytes());
}
</code></pre>
<p>How would I do the same in python?</p>

<cardano-serialization-lib><address><python>
Derive public key hash from bech32 address using python
# Derive public key hash from bech32 address using python

(Asked by: username_0 on 2022-04-25)

I know that this is possible in javascript using the cardano serialization lib, as follows:
function toHex(bytes){
    return Buffer.from(bytes).toString("hex");
}

function addrToPubKeyHash(bech32Addr) {
  const pkh = Loader.Cardano.BaseAddress.from_address(
    Loader.Cardano.Address.from_bech32(bech32Addr)
  )
    .payment_cred()
    .to_keyhash();

  return toHex(pkh.to_bytes());
}

How would I do the same in python?

**Tags:** cardano-serialization-lib, address, python

---

## 1 answers

### Answer by username_1 on 2022-04-26. Score: 3 | Accepted answer

Check out pycardano.
Class Address has a class method from_primitive that can read and decode a bech32 address. Then you can directly read field payment_part, which is the public key hash of the payment credential from the address object. You can find the API documentation here.
Example:
>>> from pycardano import Address   
>>> addr = Address.from_primitive("addr_test1vzv89dr7v69enywx0t7sqcyxshcmnlv9q43lhpg7ecfhjjsjcdsza")
>>> addr.payment_part
VerificationKeyHash(hex='9872b47e668b9991c67afd00608685f1b9fd850563fb851ece13794a')


---

