How Can I Read Inputs as Numbers in Python?

Are you ready to take your Python knowledge to the next level? Letโ€™s have a little fun with the inputs. Only instead of having you enter your name, weโ€™ll work with numbers. Itโ€™s a nice, simple calculator. You enter in two numbers and it adds them together to give you an answer. Simple, right? Well, … Read more

Python String Formatting: How to Become a String Wizard with the Format Specification Mini-Language

Python provides fantastic string formatting options, but what if you need greater control over how values are presented? Thatโ€™s where format specifiers come in.  This article starts with a brief overview of the different string formatting approaches. Weโ€™ll then dive straight into some examples to whet your appetite for using Pythonโ€™s Format Specification Mini-Language in … Read more

How to Parse JSON in a Python One-Liner?

Problem: How to Parse a JSON object as a Python One-Liner? Example: Say, you have pulled a JSON object from a server using the curl command: Source How to parse the resulting JSON object and extract, say, the value “Netherlands” associated to the “country” key? Solution: The following one-liner accomplishes this: The command consists of … Read more

What is the Asterisk / Star Operator (*) in Python?

Asterisk Operator

Many Python coders—even at intermediate skill levels—are often puzzled when it comes to the asterisk character in Python. What does it mean? How does it work? What’s it purpose? This article answers all of those questions and more. After studying this article, you will have a solid understanding of the asterisk operator * in Python—and … Read more

Python One Line to Multiple Lines

To break one line into multiple lines in Python, use an opening parenthesis in the line you want to break. Now, Python expects the closing parenthesis in one of the next lines and the expression is evaluated across line boundaries. As an alternative, you can also use the backslash \ just in front of the … Read more

How to Search and Replace a Line in a File in Python? 5 Simple Ways

Problem: Given the contents of a text file. How to search and replace a specific string or line in the file? Example: Let’s consider the following example where you want to replace the highlighted (bolded) text parts. Text in the file before replacing a line: There was an idea to bring together a group of … Read more

Python One Line Regex Match

Summary: To match a pattern in a given text using only a single line of Python code, use the one-liner import re; print(re.findall(pattern, text)) that imports the regular expression library re and prints the result of the findall() function to the shell. Problem: Given a string and a regular expression pattern. Match the string for … Read more

Python One Line Quine

Most computer scientists, programmers, and hackers don’t even know the meaning of the word “Quine” in the context of programming. So, first things first: What is a Quine? Roughly speaking, a quine is a self-reproducing program: if you run it, it generates itself. Here’s a great definition: :quine: /kwi:n/ /n./ [from the name of the … Read more