Back to Python
2026-03-305 min read

String Exercises (Python Programming)

Learn String Exercises (Python Programming) step by step with clear examples and exercises.

Title: Python String Exercises - Mastering Strings with Practical Examples and Common Mistakes

Why This Matters

In this tutorial, we will delve into Python string exercises to enhance your programming skills. Mastering strings is essential for various real-world applications, such as web development, data analysis, and system scripting. Understanding string manipulation can also help you solve complex problems during interviews or debugging issues in your code.

Strings in Python are sequences of characters enclosed within single quotes (') or double quotes ("). To perform operations on strings, you can use various built-in methods and functions. In this lesson, we will explore these methods and functions in detail with practical examples and common mistakes to avoid.

Prerequisites

To follow this tutorial, you should have a basic understanding of Python syntax and variables. If you're new to Python, we recommend checking out our Python for Beginners tutorial first.

Core Concept

String Operations

Strings in Python can be manipulated using various built-in methods and functions. Here are some common string operations:

  1. Concatenation: Joining two or more strings using the + operator.
  2. Length: Finding the number of characters in a string using the len() function.
  3. Slicing: Accessing a portion of a string using indexes.
  4. Uppercase and Lowercase: Converting a string to uppercase or lowercase using the upper() and lower() methods, respectively.
  5. Find: Locating the position of a substring within a string using the find() method.
  6. Replace: Replacing a substring with another string using the replace() method.
  7. Split: Splitting a string into a list of substrings based on a delimiter using the split() method.
  8. Format String: Formatting a string using placeholders and the format() function.

String Methods and Functions

Concatenation (+)

To concatenate two or more strings, use the + operator:

str1 = "Hello"
str2 = "World"
concatenated_string = str1 + ' ' + str2
print("Concatenated String:", concatenated_string) # Output: Hello World

Length (len())

To find the length of a string, use the len() function:

str1 = "Hello World"
print("Length of Str1:", len(str1)) # Output: 11

Slicing

To access a portion of a string using indexes, use slicing:

str1 = "Hello World"
print("Str1[0:5]:", str1[0:5]) # Output: Hello,
print("Str1[7:16]:", str1[7:16]) # Output: World

Uppercase and Lowercase (upper(), lower())

To convert a string to uppercase or lowercase, use the upper() and lower() methods, respectively:

str1 = "Hello World"
print("Uppercase Str1:", str1.upper()) # Output: HELLO WORLD
print("Lowercase Str1:", str1.lower()) # Output: hello world

Find (find())

To locate the position of a substring within a string, use the find() method:

str1 = "Hello World"
position = str1.find("World")
print("Position of 'World' in Str1:", position) # Output: 6

Replace (replace())

To replace a substring with another string, use the replace() method:

str1 = "Hello World"
new_str1 = str1.replace("World", "Universe")
print("Replaced Str1:", new_str1) # Output: Hello Universe

Split (split())

To split a string into a list of substrings based on a delimiter, use the split() method:

str1 = "Hello World, Nice to meet you!"
words = str1.split(', ')
print("Words in Str1:", words) # Output: ['Hello', 'World', 'Nice', 'to', 'meet', 'you!']

Format String (format())

To format a string using placeholders and the format() function, use the following syntax:

name = "John"
age = 25
greeting = f"Hello, {name}! You are {age} years old."
print("Formatted Greeting:", greeting) # Output: Hello, John! You are 25 years old.

Worked Example

Let's create a simple Python script that demonstrates various string operations:

Importing the required libraries

import math

Defining two strings

str1 = "Hello, World!"

str2 = "Python is awesome!"

Concatenation

concatenated_string = str1 + ' ' + str2

print("Concatenated String:", concatenated_string)

Length

print("Length of Str1:", len(str1))

print("Length of Str2:", len(str2))

Slicing

print("Str1[0:5]:", str1[0:5]) # Output: Hello,

print("Str2[7:16]:", str2[7:16]) # Output: is awesome!

Uppercase and Lowercase

print("Uppercase Str1:", str1.upper())

print("Lowercase Str2:", str2.lower())

Find

position = str1.find('World')

print("Position of 'World' in Str1:", position) # Output: 6

Replace

new_str1 = str1.replace('World', 'Universe')

print("Replaced Str1:", new_str1)

Split

words = str2.split()

print("Words in Str2:", words)

Format String

name = "John"

age = 25

greeting = f"Hello, {name}! You are {age} years old."

print("Formatted Greeting:", greeting)

Common Mistakes

  1. Forgotten Spaces: Always ensure there are no extra spaces before or after string literals, as they can cause unexpected results during concatenation or slicing operations.

Incorrect:

str1 = " Hello World! "

Correct:

str1 = "Hello World!"
  1. String Concatenation vs. Multiplication: Be aware that the * operator can be used to repeat a string, but it will not concatenate multiple strings as you might expect. To concatenate multiple strings, use the + operator instead.

Incorrect:

str1 = "Hello" * 3

Correct:

str1 = "Hello" + "World"

Practice Questions

  1. Write a Python script that takes two strings as input and returns their concatenated string, length, and the position of the first occurrence of the second string within the first one.
  1. Write a Python script that reads a file line by line, counts the number of words in each line, and calculates the average number of words per line.
  1. Write a Python script that removes all duplicate characters from a given string.

FAQ

  1. What is the difference between single quotes (') and double quotes (") in Python strings?
  • In Python, both single quotes (') and double quotes (") can be used to define strings. However, it is considered a good practice to use either single or double quotes throughout your code to avoid confusion when nesting strings.
  1. How do I escape special characters in Python strings?
  • To include special characters like ', ", and backslash (\) within a string, you can use the backslash (\\) escape character. For example:
str1 = "I said, \"Hello,\" to her."
  1. How do I check if a string is empty in Python?
  • To check if a string is empty in Python, you can use the len() function or the if str: conditional statement:
if len(str) == 0:
print("The string is empty.")
elif len(str) > 0:
print("The string is not empty.")

Or simply use the following one-liner:

if not str:
print("The string is empty.")
else:
print("The string is not empty.")
String Exercises (Python Programming) | Python | XQA Learn