beginner

Working with Strings

Learn how to create, manipulate, and format strings in Python.

Strings are one of the most commonly used data types in Python programming. They allow you to store and manipulate text data, from simple words to complex documents. Understanding string operations is essential for tasks like data processing, user input handling, and building dynamic applications. In this exercise, you will master the fundamentals of Python strings and learn techniques that are used in real-world projects every day.

📚 Concepts & Theory

Strings in Python are sequences of characters enclosed in quotes. You can use single quotes (''), double quotes (""), or triple quotes (''' or """) for multi-line strings.

Creating Strings:

name = "Alice"
message = 'Hello, World!'
paragraph = '''This is a
multi-line string.'''

Common String Operations:

  • Concatenation: Joining strings with +

  • Repetition: Repeating strings with *

  • Indexing: Accessing characters with [index]

  • Slicing: Extracting substrings with [start:end]

  • Methods: upper(), lower(), strip(), replace(), split()
F-Strings (Formatted String Literals):
name = "Bob"
age = 25
greeting = f"Hello, {name}! You are {age} years old."

F-strings were introduced in Python 3.6 and are the recommended way to format strings due to their readability and performance.

🎯 Your Challenge

Create a variable called `full_name` that combines the first name "John" and last name "Doe" with a space between them. Then create a variable called `greeting` that uses an f-string to say "Welcome, {full_name}!".

📝 Starter Code

Python
# Create full_name by combining first and last name
first_name = "John"
last_name = "Doe"

# Your code here
full_name = 

# Create greeting using f-string
greeting = 
  • Use the + operator to join strings together
  • Remember to add a space " " between names
  • F-strings start with f before the opening quote
  • Variables inside f-strings go in curly braces {variable}

Solution

Python
first_name = "John"
last_name = "Doe"

full_name = first_name + " " + last_name

greeting = f"Welcome, {full_name}!"

Explanation

We concatenate first_name and last_name using the + operator, with a space string " " between them. Then we use an f-string prefix (f"...") to embed the full_name variable directly inside curly braces {}. This creates a clean, readable greeting message.

⚠️ Common Mistakes to Avoid

  • Forgetting the space between first and last name
  • Using regular quotes instead of f-string prefix
  • Missing curly braces around variable in f-string
  • Mixing single and double quotes incorrectly

❓ Frequently Asked Questions

In Python, single quotes and double quotes work the same way for creating strings. You can use either one, but you must be consistent within a single string. Use one type when your string contains the other type.
F-strings are more readable, especially with multiple variables. They are also faster than other formatting methods and allow you to include expressions directly inside the curly braces.

🔗 Related Concepts