5 Subtle Python Built-In Command-Line Tricks That Will Make Your Life Easier

Forget bash aliases — Python’s CLI can already do more than you think.

5 Subtle Python Built-In Command-Line Tricks That Will Make Your Life Easier
Photo by davisuko on Unsplash

You don’t need a framework or third-party tool — Python’s CLI has secret weapons built right in.

5 Subtle Python Built-In Command-Line Tricks That Will Make Your Life Easier

Python is often celebrated for its clean syntax and powerful libraries. But what if I told you that without writing a single .py file, Python can become your secret weapon right from the command line?

Whether you’re a developer, data analyst, or automation enthusiast, Python’s built-in command-line options can simplify your workflow, help debug faster, and even replace quick bash scripts.

Yet, many developers overlook these subtle gems.

Here are 5 surprisingly powerful Python command-line tricks you can start using today.


1. Run Python One-Liners with -c

If you ever needed to execute a quick snippet without firing up a REPL or saving a file, -c is your best friend.

python -c "print('\n'.join(['🍕' * i for i in range(1, 6)]))"

It prints a pyramid of pizza emojis. 🍕

🍕 
🍕🍕 
🍕🍕🍕 
🍕🍕🍕🍕 
🍕🍕🍕🍕🍕

You can run anything from quick calculations to file manipulation on the fly.

Perfect for scripting inside CI/CD pipelines, shell aliases, or quick experiments.

2. Explore Any Module Interactively with -m

You’ve probably used python -m pip, but did you know you can run any module this way?

Try this:

python -m http.server 8000

This spins up a simple HTTP server in your current directory — instantly.

Other examples:

  • Run a profiler: python -m cProfile my_script.py
  • Compile Python files: python -m compileall .
  • Launch a debugger: python -m pdb script.py
Tip: Use python -m this for a fun Easter egg.

3. Use -m timeit to Benchmark Code Snippets

Want to know which approach is faster — join() vs. + for string concatenation?

python -m timeit "'-'.join(['a'] * 1000)"

Or:

python -m timeit "'a' * 1000"

You’ll get real benchmarking results without needing a stopwatch or writing test functions.

100000000 loops, best of 5: 2.9 nsec per loop

Micro-optimizations matter when scaling or doing data-heavy operations. And timeit eliminates the guesswork.


4. Read Files with python -c and open()

Need to read or modify a file quickly?

python -c "print(open('requirements.txt').read())"

Or even update a file in place:

python -c "open('file.txt', 'a').write('\n# Appended by Python')"

Use -i along with -c to drop into an interactive shell after the script executes:

python -i -c "data = open('file.txt').read()"

Now you can play around with data interactively.


5. Use Python as a Pocket Calculator

Ditch the calculator app. Python’s math module makes your terminal a scientific calculator.

python -c "import math; print(math.sqrt(144))"

Even better, define quick aliases in your shell:

alias pycalc='python3 -c "import math; print("'

Now you can do things like:

pycalc "math.sin(math.pi / 2)"

It’s more accurate than most CLI calculators and fully scriptable.


Bonus: The Hidden REPL Gem — python -q

Want to suppress the startup banner when launching the Python shell?

python -q

Clean, distraction-free REPL — ideal when using Python interactively in scripts or pipes.


Final Thoughts

The command line is where Python quietly shines. These subtle tricks won’t make flashy headlines, but they can save time, eliminate boilerplate, and streamline everyday tasks.

Next time you’re tempted to write a whole script for something trivial, try reaching for the command line instead.


Let’s Make Python Work for You

If you enjoyed this, follow me for more Python tips, dev workflows, and practical coding habits. Let’s squeeze more power out of every line you write.

Which of these tricks did you already know? Got a favorite Python command-line hack of your own? Drop it in the comments!

Photo by Toa Heftiba on Unsplash