Python String Exercises

Python String Exercises

In Python, a string is a sequence of characters enclosed in either single quotes (”) or double quotes (“”). Strings are immutable, which means they cannot be changed after they are created. Python provides a variety of built-in functions and methods that allow you to manipulate and work with strings effectively. In this article, we will explore some common string exercises and provide examples to help you understand how to use them.

Exercise 1: String Concatenation

String concatenation is the process of combining two or more strings into a single string. In Python, you can concatenate strings using the ‘+’ operator. Here’s an example:

str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result)

Output:

Hello World

Exercise 2: String Length

To find the length of a string in Python, you can use the built-in len() function. It returns the number of characters in the string. Here’s an example:

str = "Python"
length = len(str)
print(length)

Output:

6

Exercise 3: String Slicing

String slicing allows you to extract a portion of a string by specifying the start and end indices. The start index is inclusive, while the end index is exclusive. Here’s an example:

str = "Python"
substring = str[1:4]
print(substring)

Output:

yth

Exercise 4: String Formatting

String formatting allows you to create dynamic strings by inserting variable values into a predefined template. Python provides several ways to format strings, including the ‘%’ operator, the format() method, and f-strings. Here’s an example using f-strings:

name = "Alice"
age = 25
greeting = f"Hello, my name is {name} and I am {age} years old."
print(greeting)

Output:

Hello, my name is Alice and I am 25 years old.

Exercise 5: String Methods

Python provides a wide range of built-in string methods that allow you to perform various operations on strings. Here are a few commonly used string methods:

  • lower(): Converts all characters in a string to lowercase.
  • upper(): Converts all characters in a string to uppercase.
  • strip(): Removes leading and trailing whitespace from a string.
  • replace(): Replaces a specified substring with another substring.
  • split(): Splits a string into a list of substrings based on a specified delimiter.

Here’s an example that demonstrates the usage of these string methods:

str = "   Python Programming   "
lowercase = str.lower()
uppercase = str.upper()
stripped = str.strip()
replaced = str.replace("Python", "Java")
splitted = str.split()
print(lowercase)
print(uppercase)
print(stripped)
print(replaced)
print(splitted)

Output:

python programming
PYTHON PROGRAMMING
Python Programming
   Java Programming   
['Python', 'Programming']

These were just a few examples of the many exercises you can practice to improve your string manipulation skills in Python. By understanding and utilizing the various string functions and methods, you can effectively work with strings in your Python programs.

Scroll to Top