<p>Yes:</p>

<pre><code>import ecdsa
print(ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1).to_string())
</code></pre>

CC BY-SA 3.0
2016-11-10T18:44:30.123Z
Thanks for answer! So, when I'm runnnig your code, I'm getting something like `b'\xe9+\xf7\x97A\.....`. How can I convert it to a usual string? `str()` and `.decode('utf-8')` don't work :(
Sergey Potekhin
CC BY-SA 3.0
2016-11-10T18:51:19.363Z
It's a `bytes` type. Interpreting it as ascii or as UTF-8 doesn't make any sense. Are you trying to convert to hex, or are you trying to print raw bytes to the terminal?
Nick ODell
CC BY-SA 3.0
2016-11-10T20:45:48.990Z
Well, I mean that usually ECDSA private key looks like `18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725` according to [this](https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses). So, how can I get an address which looks the same?
Sergey Potekhin
CC BY-SA 3.0
2016-11-10T21:00:56.840Z
*a key which looks the same
Sergey Potekhin
CC BY-SA 3.0
2016-11-10T22:53:53.267Z
Use `binascii.hexlify(key.to_string()).decode('ascii').upper()`
Nick ODell
CC BY-SA 3.0
2016-11-10T18:08:12.733Z
2016-11-10T18:08:12.733Z
Nick ODell
<p>Pretty same question was asked <a href="https://bitcoin.stackexchange.com/questions/13970/python-code-to-generate-private-ecdsa-key">here</a>, but maybe there is a way to generate keys using <a href="https://github.com/warner/python-ecdsa" rel="nofollow noreferrer">ecdsa</a> module in just a few lines of code?</p>

<address><ecdsa><python>
How to generate Bitcoin private - public keys with ECDSA module?
Sergey Potekhin
# How to generate Bitcoin private - public keys with ECDSA module?

(Asked by: username_0 on 2016-11-10)

Pretty same question was asked [here](https://bitcoin.stackexchange.com/questions/13970/python-code-to-generate-private-ecdsa-key), but maybe there is a way to generate keys using [ecdsa](https://github.com/warner/python-ecdsa) module in just a few lines of code?

**Tags:** address, ecdsa, python


---

## 1 answers

### Answer by username_1 on 2016-11-10. Score: 3 | Accepted answer

Yes:
import ecdsa
print(ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1).to_string())

> **Comments:**
> **username_0**: Thanks for answer! So, when I'm runnnig your code, I'm getting something like `b'\xe9+\xf7\x97A\.....`. How can I convert it to a usual string? `str()` and `.decode('utf-8')` don't work :(  
> **username_1**: It's a `bytes` type. Interpreting it as ascii or as UTF-8 doesn't make any sense. Are you trying to convert to hex, or are you trying to print raw bytes to the terminal?  
> **username_0**: Well, I mean that usually ECDSA private key looks like `18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725` according to [this](https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses). So, how can I get an address which looks the same?  
> **username_0**: *a key which looks the same  
> **username_1**: Use `binascii.hexlify(key.to_string()).decode('ascii').upper()`  

---

