
PYTHON — String Intro Exercise Python
The ultimate promise of technology is to make us master of a world that we command by the push of a button. — Volker Grassmuck
Strings are a fundamental data type in Python that allows the storage and manipulation of text. In this exercise, we will explore different ways to create and manipulate strings. The exercise involves printing four individual strings that fulfill certain conditions.
Exercise Task
- Create a string with double quotation marks inside it.
- Create a string with an apostrophe inside it.
- Create a string with multiple lines and preserved whitespace.
- Create a string on multiple lines but print it on a single line.
Let’s solve each of these tasks step by step using Python.
Task 1: String with Double Quotation Marks
string_with_quotes = "I am surrounded by \"double quotes\""
print(string_with_quotes)Task 2: String with Apostrophe
string_with_apostrophe = 'There\'s an apostrophe in this string'
print(string_with_apostrophe)Task 3: String with Multiple Lines
multi_line_string = """This is a string
that spans
multiple lines"""
print(multi_line_string)Task 4: Multi-Line String Printed on a Single Line
multi_line_single_print = ("This string is coded on "
"multiple lines but printed on a single line")
print(multi_line_single_print)By completing these tasks, you have explored different ways of constructing strings in Python, including handling quotes, apostrophes, multiple lines, and printing strings on a single line.
This exercise provides a practical way to remind yourself how to create strings that fulfill specific conditions. You can now compare your solutions with the next lesson, where the tasks will be solved.
Strings are a versatile and essential part of Python programming, and understanding how to work with them effectively is crucial for any Python developer.
