How to generate a new Ethereum address in Python | QuickNode (2024)

4 min read

Overview

Python is one of the most versatile programming languages out there with an abundance of use cases; We can build many applications with Python from client-side to back end. In this guide, we will cover creating an Ethereum address in Python using the Web3Py library.

Prerequisites

  • Python installed in your system (version 3.6+) and Pip3.
  • A text editor.

What is an Ethereum address?

While signing in to any platform on the internet, you need a combination of username and password. Think of Ethereum address as a username with a corresponding private key as the password. Using this combination of address and private key lets you interact with the Ethereum blockchain. An Ethereum address is your identity on the blockchain. It looks like this “0xd5e099c71b797516c10ed0f0d895f429c2781142”, Ethereum address is public and can be shared, but the private key must always be kept secret. Ethereum addresses are generated using a private key. The following is the process of how an Ethereum address is generated:

  • Generate a random private key of (64 (hex) characters / 256 bits / 32 bytes)
0xf4a2b939592564feb35ab10a8e04f6f2fe0943579fb3c9c33505298978b74893
  • A Public key is derived from the private key (128 (hex) characters / 512 bits / 64 bytes) using Elliptic Curve Digital Signature Algorithm (ECDSA)
0x04345f1a86ebf24a6dbeff80f6a2a574d46efaa3ad3988de94aa68b695f09db9ddca37439f99548da0a1fe4acf4721a945a599a5d789c18a06b20349e803fdbbe3
  • Then Keccak-256 hash function is applied on (128 characters / 64 bytes) public key, which gives out a (64 characters / 32 bytes) hash string, the last 40 characters / 20 bytes when prefixed with 0x is the Ethereum address.

Following are the few things that need to have an Ethereum address:

  • Receiving/Sending ETH.
  • Sign/Send transactions.
  • Connecting to Applications.

What is Python?

Python is a general-purpose programming language that has a wide range of applications. It has high-level data structures. It is dynamically typed. It has a dynamic binding and many more features, making it a handy tool to develop complex applications as it is used for scripting or “glue code” that connects different components. It can also make system calls on almost all operating systems. Python is a universal language due to its ability to run on nearly every system architecture and omnipresence. Python is an interpreted, interactive, object-oriented programming language.

What is Web3Py?

Web3.py is a Python library that helps you interact with the Ethereum blockchain; using web3Py, one can make backend clients for their decentralized applications (dApps) to handle interaction with blockchain, reading data from it, writing transactions, or executing smart contract logic. The original API was derived from Web3.js JavaScript API but has improved with time to better serve and cater to Python Developers' needs and demands.

Since we are working with Python here, we’ll use Web3.py.

Now, let’s see how we can generate a new Ethereum address in Python.

Generating an Ethereum address in Python

Our first step here would be to check if Python 3.6 or higher is installed on your system; you can check if Python is installed on not by typing the following in your terminal/cmd:

$ python --version

If not installed, you can follow the instructions on the Downloads page of Python’s official website.

We’ll use Web3Py, a Python library used to interact with Ethereum. We’ll install Web3Py using PIP type the following in your terminal/cmd:

$ pip install web3

Note: Python and other library versions cause common installation problems. Therefore, if you face any problem, try setting up a virtual environment and troubleshoot the web3.py installation.

If everything goes right, Web3.py will be installed in your system.

Now, let’s create a Python file and name it address.py, copy-paste the following code into the file:

from eth_account import Account
import secrets
priv = secrets.token_hex(32)
private_key = "0x" + priv
print ("SAVE BUT DO NOT SHARE THIS:", private_key)
acct = Account.from_key(private_key)
print("Address:", acct.address)

Explanation of the code above

Line 1: Importing Account from the eth_account module of Web3.py

Line 2: Importing Python's secrets module, which will help us generate a random hexadecimal string.

Line 3: Generating a random hexadecimal string of 32 bytes / 64 characters and storing it in priv variable

Line 4: Attaching 0x prefix to our 64 character hexadecimal string stored in priv and storing the new string in variable private_key.

Line 5: Printing our Private key with a warning.

Line 6: Creating a new account using the private_key and storing it in variable acct.

Line 7: Printing the address of the account stored in acct variable with a string “Address:”

Now, let’s run our Python program.

$ python address.py

If you followed the instructions correctly, it must give out an output something like this. The first line consists of the private key, and the second line consists of the Ethereum address.

How to generate a new Ethereum address in Python | QuickNode (1)

Conclusion

With your own Ethereum address (+ private key) now, you can send transactions and interact with smart contracts, refer to Web3.py’s official documentation for more information.

Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you’ll ever meet :)

How to generate a new Ethereum address in Python | QuickNode (2024)
Top Articles
What are the Major Goals of Artificial Intelligence?
Fix double NAT when two routers run at the same time
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Kelle Weber

Last Updated:

Views: 5279

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.