Keep your place in this quest

Log in or sign up for free to subscribe, follow lesson progress, and access more learning content.

Welcome to the Strings and Formatting lesson!

Here, we’ll explore the basics of working with strings in Python — how to create them, join them together, extract parts, and insert variables into them without messy concatenation.


What Is a String?

In Python, a string is simply a sequence of characters wrapped in quotes.
It can be single quotes ('Hello') or double quotes ("Hello"), both work the same way.

Example:

name = "John"

You can store names, messages, numbers-as-text, or even empty strings ("") inside variables. Strings are one of the most common data types in programming — you’ll use them for everything from user input to displaying results.

TIP: Use double quotes if your text contains an apostrophe, like "I'm learning Python".

Concatenating Strings

Concatenation is the act of joining two or more strings together to form a single one. In Python, you can do this with the + operator.

Example:

first_name = "John"
last_name = "Smith"

full_name = first_name + " " + last_name
print(full_name)

Output:

John Smith

Here, " " is just a space string, so we don’t end up with "JohnSmith".

Note: The + operator only works if both items are strings — combining a string with a number will give you an error unless you convert the number to a string with str().

Slicing Strings

Sometimes you don’t need the whole string, just a part of it. In Python, you can “slice” a string by specifying start and end positions inside square brackets:

phrase = "Learning Python is fun"

first_word = phrase[0:8]
print(first_word)

Output:

Learning
  • The first number is the starting index (0 for the very first character).
  • The second number is the end index, but it’s exclusive — meaning it stops right before that position.
TIP: You can omit the start or end index to slice from the beginning or to the end.
Example: phrase[:8] → "Learning"

Formatting Strings

Formatting strings means inserting variables into a string without having to concatenate them manually.

Method 1: .format()

name = "John"
age = 30

sentence = "My name is {} and I am {} years old.".format(name, age)
print(sentence)

Method 2: Old % Style

sentence = "My name is %s and I am %s years old." % (name, age)

Method 3: f-strings (Python 3.6+)

sentence = f"My name is {name} and I am {age} years old."

All three examples output:

My name is John and I am 30 years old.
IMPORTANT!: F-strings are generally preferred today because they’re shorter, cleaner, and easier to read.

Wrapping Up

You’ve just learned three essential skills for working with strings:

  • Concatenation — joining strings together.
  • Slicing — extracting parts of a string.
  • Formatting — embedding variables inside strings neatly.

Each of these tools shines in different situations, and you’ll find yourself using them constantly as you build Python programs.

In the next lesson, we’ll look at lists and tuples — powerful ways to store and manage collections of data.