Python String Programs with Solutions

Python Practice › String Programs

Python String Programs with Solutions

106 programs101 easy5 medium0 hard

106 string programs that cover slicing, searching, counting, replacing, splitting and joining text. They are among the most common Python interview questions, and each one has an explained solution with real output.

Before you start

Read the Strings & Methods and String Formatting & f-strings lesson first if the topic is new to you.

All programs

Python OS Module Programs with Solutions

Python Practice › OS Module Programs

Python OS Module Programs with Solutions

49 programs47 easy2 medium0 hard

49 programs for the os module and its friends os.path, shutil and subprocess: folders, paths, file counts, copying, backups, permissions, environment variables and running commands. Every solution works inside its own sample folder, so it is safe to run.

Before you start

Read the OS Module and Modules & pip lesson first if the topic is new to you.

All programs

Combine two lists

Python Practice › List Programs › Program 2

Combine two lists

EasyList Programs · 2 of 100

Write a Python program to combine two lists.

Input
[1, 2, 3], [4, 5, 6]
Output
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

Approach

  1. + creates a new list; extend() adds to an existing one.

Solution

Python
list1 = [1, 2, 3]list2 = [4, 5, 6]print(list1 + list2)list1.extend(list2)print(list1)
Output
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

Line by line

Line 3

+ returns a new list and leaves both originals unchanged.

Line 4

extend changes list1 in place.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Square of each number in a list

Python Practice › List Programs › Program 1

Square of each number in a list

EasyList Programs · 1 of 100

Write a Python program to calculate the square of each number in a list.

Input
[2, 4, 6, 8, 10]
Output
[4, 16, 36, 64, 100]

Approach

  1. Loop over the list and append each number squared, or use a list comprehension.

Solution 1: loop

Python
numbers = [2, 4, 6, 8, 10]squares = []for n in numbers:    squares.append(n ** 2)print(squares)
Output
[4, 16, 36, 64, 100]

Line by line

Line 2

Start with an empty list for the results.

Lines 3-4

n ** 2 squares each number and append adds it to the end.

Solution 2: list comprehension

Python
numbers = [2, 4, 6, 8, 10]print([n ** 2 for n in numbers])
Output
[4, 16, 36, 64, 100]

Line by line

Line 2

The comprehension does the same loop in one expression.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Python Pandas Programs with Solutions

Python Practice › Pandas Programs

Python Pandas Programs with Solutions

95 programs95 easy0 medium0 hard

95 pandas programs: Series arithmetic and statistics, building and filtering DataFrames, sorting, adding and renaming columns, string methods, joining tables and handling missing values. Outputs come from pandas 3; the in-browser Try it uses an older pandas, so some printouts (such as column types) may differ slightly.

Before you start

Read the Pandas lesson first if the topic is new to you.

All programs

Python Tuple Programs with Solutions

Python Practice › Tuple Programs

Python Tuple Programs with Solutions

70 programs65 easy5 medium0 hard

70 tuple programs: creating, indexing, unpacking, sorting lists of tuples, converting between tuples, lists, strings and dicts, and filtering tuples by their contents. Each solution is explained and shows its real output.

Before you start

Read the Tuples and List vs Tuple lesson first if the topic is new to you.

All programs

Python File Handling Programs with Solutions

Python Practice › File Handling Programs

Python File Handling Programs with Solutions

38 programs38 easy0 medium0 hard

38 file handling programs: reading, writing and appending, working line by line, counting words and characters, copying with changes, and moving the cursor. Every solution first creates its own sample file, so you can run it anywhere, including with the Try it button.

Before you start

Read the File Handling and Read/Write JSON and Read/Write CSV lesson first if the topic is new to you.

All programs

Python If-Else Programs with Solutions

Python Practice › If-Else Programs

Python If-Else Programs with Solutions

64 programs59 easy5 medium0 hard

64 if-else programs that practise decisions in Python: comparisons, elif chains, nested conditions and combining tests with and / or. Each solution tests several inputs so you can see every branch run.

Before you start

Read the If…Else and Match Case lesson first if the topic is new to you.

All programs

Python Basic Programs with Solutions

Python Practice › Basic Programs

Python Basic Programs with Solutions

40 programs34 easy6 medium0 hard

Start here if you are new to Python. These 40 basic programs use only variables, arithmetic, a few built-in modules and print(), and each one comes with an explained solution you can run in the browser.

Before you start

Read the Variables and Numbers & Math and Operators & Booleans lesson first if the topic is new to you.

All programs

Python Function Programs with Solutions

Python Practice › Function Programs

Python Function Programs with Solutions

39 programs35 easy4 medium0 hard

39 function programs that practise defining functions, parameters, return values, *args, nested functions and small menu-driven systems. Each solution is written as a reusable function and tested with several inputs.

Before you start

Read the Functions and Lambda and Scope lesson first if the topic is new to you.

All programs