What is the Zen of Python?

The Zen of Python is an Easter Egg that long time Pythoneer (Tim Peters) channeled the guiding principals for the language’s design principals into 20 aphorisms, of which only 19 of them are written down. How to access this Zen of Python Easter Egg By importing this into your Python application, it will immediately print as follows: import this What is the Zen of Python? Beautiful is better than ugly. Explicit is better than implicit....

March 30, 2023 · 1 min · 202 words · Andrew

How to Create a Password Generator in C++

You can easily create a password generator in C++ with the following simple method. How to create the Password Generator in C++ #include <iostream> #include <string> #include <algorithm> #include <random> std::string generate_password(int length = 16) { std::string seed = string("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + string("0123456789") + string("[email protected]#$%^&*()_+=-{}[]\\|:;<>,.?/"); std::string password_string = seed; std::shuffle(password_string.begin(), password_string.end(), std::mt19937{std::random_device{}()}); return password_string.substr(0, length); } int main() { std::cout << generate_password() << std::endl; std::cout << generate_password(28) << std::endl; return 0; }

March 27, 2023 · 1 min · 71 words · Andrew

How to read user input as numbers in Python

In Python 3 onwards, the input function returns a string type.. int value You can explicitly convert this into an int type. val = int(input("Enter a number: ")) float value If you need to accept fractional components, then simply swap this out with a float type: val = float(input("Enter a number:")) map multiple values If you have multiple integers in a single line, then you can use map to extract them into a list:...

March 23, 2023 · 1 min · 78 words · Andrew

How to Capitalize the first letter of each word in a string in Python

If you have a sentence containing multiple words, and you want each of the words to start with a capital letter, then you can do one of the following: Option 1 - using string.capwords() import string string.capwords('this is a test!') Output: 'This Is A Test!' Option 2 - using title() 'this is a test!'.title() Output: 'This Is A Test!' Option 3 - using join(), split() and list comprehensions " ".join(w.capitalize() for w in 'this is a test!...

March 22, 2023 · 1 min · 82 words · Andrew

How to Create a Password Generator in Python

You can easily create a password generator in Python with the following simple method. How to create the Password Generator in Python import string import random def generate_password(length=16): seed = f"{string.ascii_letters}{string.ascii_lowercase}{string.ascii_uppercase}{string.punctuation}" password_string = [x for x in seed] random.shuffle(password_string) return ''.join(password_string[:length]) How to use the Password Generator in Python With this function, you can either call the generate_password() function without any arguments, and it will generate a 16 digit long password, unless you need a longer one, then pass in an integer to specify the lengh of the password you would like....

March 16, 2023 · 1 min · 115 words · Andrew

How to Convert Bytes to a String in Python

If you need to convert bytes to a string in Python, then you can do the following: your_string = b'This works \xE2\x9C\x85'.decode("utf-8") print(your_string) Output: This works ✅ Note that in the above example, we have converted a utf-8 string to.

March 14, 2023 · 1 min · 40 words · Andrew

How to measure the elapsed time in Python

Option 1 - using the time module import time start = time.time() print(23*2.3) end = time.time() print(end - start) Output: 52.9 3.600120544433594e-05 Option 2 - using the timeit module from timeit import default_timer as timer start = timer() print(23*2.3) end = timer() print(end - start) Output: 52.9 6.355400000000039e-05

March 13, 2023 · 1 min · 48 words · Andrew

How to get the SHA512 sum of a string using Python

If you need to get the SHA512 sum of a string using Python, then you can do the following. Step 1 Firstly, we need to use the hashlib module: import hashlib Step 2 Now make sure that the input string is encoded correctly. your_input = "this is your input string".encode('utf-8') Step 3 Finally use the sha512 function of the hashlib module, and get the hexdigest() value from it: hashlib.sha512(your_input).hexdigest() Putting it all together import hashlib your_input = "this is your input string"....

March 2, 2023 · 1 min · 89 words · Andrew

How to get the SHA256 sum of a string using Python

If you need to get the SHA256 sum of a string using Python, then you can do the following. Step 1 Firstly, we need to use the hashlib module: import hashlib Step 2 Now make sure that the input string is encoded correctly. your_input = "this is your input string".encode('utf-8') Step 3 Finally use the sha256 function of the hashlib module, and get the hexdigest() value from it: hashlib.sha256(your_input).hexdigest() Putting it all together import hashlib your_input = "this is your input string"....

March 1, 2023 · 1 min · 89 words · Andrew

How to get the MD5 sum of a string using Python

If you need to get the MD5 sum of a string using Python, then you can do the following. Step 1 Firstly, we need to use the hashlib module: import hashlib Step 2 Now make sure that the input string is encoded correctly. your_input = "this is your input string".encode('utf-8') Step 3 Finally use the md5 function of the hashlib module, and get the hexdigest() value from it: hashlib.md5(your_input).hexdigest() Putting it all together import hashlib your_input = "this is your input string"....

February 28, 2023 · 1 min · 89 words · Andrew