len() Function in Python (With Examples)

len() function in Python: Command Prompt output of len on a string, list, tuple, dictionary, set and range

The len() function in Python returns how many items an object holds: characters in a string, elements in a list or tuple, keys in a dictionary. It works on any sequence or collection, and it’s instant, because Python stores the size rather than counting: Every output here comes from Python 3.12.5 in the Windows Command … Read more >>

How to Initialize a Python Dictionary with Keys and Values

Command Prompt output of dict(zip()) building store sales

Plenty of tutorials still initialize a Python dictionary with keys and values by filling an empty {} in an index loop, or use dict.fromkeys(keys, []), which shares one list. There’s a cleaner route. If your keys and values sit in two lists, dict(zip(keys, values)) builds the dictionary in one line. Below, I’ll show you four … Read more >>

Remove Special Characters Except Space From a String in Python

Python remove special characters except space: Command Prompt output of re.sub cleaning five customer reviews

If you’ve tried stripping symbols with isalnum() or \W, you’ve probably watched the spaces vanish too, leaving Greatproduct55. To remove special characters except space in Python, use re.sub(r”[^A-Za-z0-9 ]+”, “”, text). Why? A space isn’t a letter or a digit, so both checks delete it like any other symbol. In this article, I’ll show you … Read more >>

How to Divide Each Element in a List by a Number in Python

Divide each element in a list by a number in Python: Command Prompt output of a list comprehension

To divide each element in a list by a number in Python, use a list comprehension: [x / n for x in values]. It builds a new list, and the original isn’t changed: Every output on this page comes straight from my own machine, running Python 3.12.5 and NumPy 2.5.3 in Command Prompt. You’ll also … Read more >>

Python While Loop continue: Skip Iterations the Right Way

Python while loop continue: Command Prompt output of a loop skipping cancelled orders

In a Python while loop, continue skips the rest of the current pass and jumps straight back to the condition check. The one rule to remember: update your counter before the continue, or the loop never moves forward: Every example here ran on Python 3.12.5, plus Python 3.14 for one warning that’s new in that … Read more >>

How to Reverse a Dictionary in Python

Python reverse dictionary: Command Prompt output of a dict comprehension swapping state codes and names

To reverse a dictionary in Python, swap each key and value with a dictionary comprehension: {v: k for k, v in d.items()}. That works when the values are unique and hashable. If you want the same pairs in reverse order instead, use dict(reversed(d.items())): Every output below came from real runs on Python 3.12.5, using state … Read more >>

How to Replace a Specific Line in a File Using Python

Python replace specific line in file: Command Prompt output of line 3 changed with readlines and writelines

To replace a specific line in a file using Python, read every line into a list with readlines(), assign new text to that line’s index, then write the list back with writelines(). Python can’t overwrite one line in place, so the file is always rewritten: Every output below comes from a real run on Python … Read more >>

100+ Python Interview Questions and Answers (2026)

Python interview questions and answers: Command Prompt output of the mutable default argument and closure traps

These Python interview questions and answers are the ones I’d expect in a 2026 interview, from fresher basics to OOP, generators, the GIL and Python 3.14. Each answer is the short version an interviewer wants, plus the follow-up trap that often comes next. Warm up with this one. If you can explain why it changes … Read more >>

How to Find Numbers in a String Using Python

Find Number in String Python Using findall() Method

I was working on a project where I had to extract numerical data from messy text files. These files contained customer IDs, invoice numbers, and ZIP codes, all mixed with text. If you’ve ever worked with unstructured data in Python, you know how tricky it can be to find numbers hidden inside strings. I’ve faced … Read more >>