• Home
  • Python
    • Introduction to Python
    • Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • React.js
    • Introduction to React
    • React Developer
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • Python
      • Introduction to Python
      • Python Developer
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
    • React
      • Introduction to React
      • React Developer
  • Interactive Training
  • Pricing
  • Brainstorm

Quick Links

  • About Us
  • Pricing
  • Partnership
  • Brainstorm
  • Terms
  • Privacy
  • Refunds

Courses

  • Python
    • Introduction to Python
    • Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • React
    • Introduction to React
    • React Developer

Newsletter

Subscribe to our free monthly newsletter, for a quick update on Python, JavaScript, and React news

© 2025 - 2026 STEMTrainingGrounds. All Rights Reserved.

Introduction to Python - Lesson 6

Lesson 6 of 16

String Operations

Lesson Progress: 0%

Lesson Progress: 0%
String Operations
Lesson Incomplete
← Previous: Lists
Lesson 6 of 16
Next: String Formatting →
Code Example
# Joining two simple words
print("Data" + "Science")

# Adding a space between words
print("Python" + " " + "Programming")

# Building a simple phrase
print("Ice" + "Cream")

# Adding punctuation to a word
print("Warning" + "!")

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Python can join words together.
  • We use the plus sign + to join text.
  • Each word must be inside quotes " ".
  • print() shows the joined words on the screen.
  • print("Data" + "Science") joins two words.
  • If you want a space between words, you add " " in the middle.
  • print("Python" + " " + "Programming") adds a space.
  • You can build short phrases by joining words.
  • print("Ice" + "Cream") makes one new word.
  • You can also add symbols like ! to words.
  • print("Warning" + "!") adds an exclamation mark.
  • Each line joins text and prints the result.
Code Example
# Repeating a word multiple times
print("Hello" * 3)

# Creating a visual divider
print("-" * 10)

# Repeating a single character for emphasis
print("A" * 5)

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • In Python, the star symbol * isn't just for math; it can also copy words!
  • When you put a star and a number after a word, Python repeats that word for you.
  • For example, "Hello" * 3 tells Python to write "Hello" three times in a row.
  • You can use this to make long lines, like "-" * 10, to separate your work.
  • Just remember: to see the result on your screen, you still need to put everything inside a print() helper.
Code Example
# Indexing - Access a specific character
# Gets the first letter "P"
print("Python"[0])
# Gets the fifth letter "o"
print("Hello"[4])

# Access the first item in the list
pets = ["Cat", "Dog", "Bird", "Fish", "Hamster"]
print(pets[0])

# Access the fourth item in the list
cities = ["New York", "London", "Paris", "Tokyo", "Berlin"]
print(cities[3])

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • We use square brackets [ ] with a number to pick one letter or one item.
  • Counting starts at 0, so [0] means the first one.
  • The first item in your list is always number 0, the second is number 1, and the third is number 2.
  • For a string the first character in your list is always number 0, the second is number 1, and the third is number 2.
  • This works for both words, strings, or lists.
Code Example
# Slicing - Access a range of characters
# (indices 1, 2, and 3)
print("Weather"[1:4])
# (indices 2, 3, and 4)
print("Ocean"[2:5])

colors = ["red", "blue", "green", "yellow"]
print(colors[1:3])

fruits = ["orange", "apple", "banana", "grape"]
print(fruits[1:4])

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Slicing is like using a pair of scissors to cut out just the part of a word or list you want to keep.
  • Inside the square brackets [ ], we use two numbers with a colon in the middle to show where to start and stop.
  • Python starts counting from 0, so the first number tells Python which character or item to start with.
  • The second number tells Python where to stop, but it doesn't include that last item—it stops right before it.
  • You can use this to grab the middle of a word like "eat" out of "Weather" or to pick just two colors out of a long list.
Code Example
# Step Slicing - Reverse a string

# Reverses the word
print("stop"[::-1])

# Reverses the phrase
print("Step on no pets"[::-1])

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • You can use a special trick with square brackets [::-1] to flip things backward.
  • When you use this trick on a word like "stop," Python turns it into "pots."
  • It works just like a mirror! It takes the last letter and moves it to the very front.
Code Example
# Step Slicing - Reverse a string

# Step Slicing - Reverse a list
games = ["chess", "soccer", "tennis"]
print(games[::-1])

# Step Slicing - Reverse a list
fruits = ["apple", "banana", "orange"]
print(fruits[::-1])

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • You can use a special trick with square brackets [::-1] to flip things backward.
  • This trick doesn't just work for words; you can also use it to flip a whole list of games or fruits.
  • Just remember to include both colons and the minus one -1 to tell Python to walk backward.
Code Example
# More slicing

# stop at the second to last character
print("Weather"[:-2])

# stop at the third to last character
print("Weather"[:-3])

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • [:-2] means Python shows everything except the last two characters.
  • [:-3] means Python shows everything except the last three characters.
  • This is called slicing, which means showing only part of a word or list.
Code Example
# More slicing

# start at the second character
print("Weather"[1:])

# start at the third character
print("Weather"[2:])

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • [1:] means Python starts at the second character and shows the rest.
  • [2:] means Python starts at the third character and shows the rest.
  • This is called slicing, which means showing only part of a word or list.
Code Example
# More slicing

# Stop at the third element
langs = ["Python", "Java", "C++", "Ruby", "Swift"]
print(langs[0][0:2])

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • langs[0][0:2] first picks the first word in the list, then shows only its first two letters.
  • words[::-1] tells Python to go through the list backwards.
Code Example
# Reverse the list
words = ["Apple", "Bread", "Candy", "Donut", "Email"]
print(words[::-1])

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • words[::-1] tells Python to go through the list backwards.
  • This is called slicing, which means showing only part of a word or list.
Code Example
# Convert to all uppercase letters
print("hello".upper())
print("fast car".upper())

# Convert to all lowercase letters
print("PYTHON".lower())
print("DO NOT YELL".lower())

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Python can change how words look.
  • .upper() makes all letters BIG.
  • .lower() makes all letters small.
Code Example
# Capitalize every word (Title Case)
print("jane doe".title())
print("the big bridge".title())

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Python can change how words look.
  • .title() makes each word start with a big letter.
Code Example
# Capitalize only the first letter
print("apple".capitalize())
print("today is sunny".capitalize())

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • .capitalize() makes only the first letter big.
Code Example
# Removing spaces from both ends
print("   Hello   ".strip())

# Removing stars from both ends
print("***Welcome***".strip("*"))

# Removing dashes from both ends
print("---Good job!---".strip("-"))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • The .strip() helper is like a vacuum cleaner that sucks up extra spaces from the start and end of your words.
  • If you want to remove something else, like stars * or dashes -, you just put them inside the parentheses.
  • It’s perfect for tidying up your text so that only the important part in the middle is left!
Code Example
# Returns 2 (the index of the first "t")
print("Python".find("t"))

# Returns 7 (the index where "World" starts)
print("Hello World".find("World"))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • .find() tells us where a word or letter starts (it gives a number, known as the index).
  • .find() tells us the index of a word or letter in a list (it gives a number).
  • These work with words (strings) and with lists.
Code Example
# Returns 2 (there are two "p"s)
print("apple".count("p"))

# Returns 5 (there are five "e"s)
print("bee keeper".count("e"))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • .count() tells us how many times something appears.
  • Counting starts at 0, so 0 means the first spot.
  • These work with words (strings) and with lists.
Code Example
# Changing one word to another
print("I like cats".replace("cats", "dogs"))

# Fixing a name
print("Hello Sam".replace("Sam", "Alex"))

# Swapping a word in a short sentence
print("Good morning".replace("morning", "night"))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • The .replace() helper works like a magic wand that swaps one piece of text for another.
  • Inside the parentheses, you first tell Python the "old" word you want to find, then the "new" word you want to put in its place.
  • It is a fast way to fix mistakes or change the whole meaning of a sentence, like turning "I like cats" into "I like dogs" in one step!
Code Example
# Splits into parts by the space
print("Small medium large".split())

# Splits into parts by the hyphen
print("State-of-the-art".split("-"))

# Splits into parts by the slash
print("2025/12/25".split("/"))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • .split() breaks a sentence into smaller parts.
  • By default, it splits at spaces.
  • You can tell it to split at other characters like - or /.
  • print() shows the pieces on the screen.
Code Example
# .join() - Combining characters within a string

# Connects characters with a dash
print("-".join("12345"))

# Connects characters with a dot
print(".".join("SOS"))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • The .join() helper acts like glue to stick different parts together into one long string.
  • The symbol you put in quotes at the start, like a dash "-" or a star "*", is the "glue" that goes between each part.
  • You can use it to put dots between letters, like turning SOS into S.O.S.
Code Example
# Connects characters with a star
print("*".join("MAGIC"))

# Joining a list of words with a hyphen
print("-".join(["Red", "Green", "Blue"]))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • The .join() helper acts like glue to stick different parts together into one long string.
  • It also works great for lists; you can take separate words like "Red" and "Blue" and join them into one line.
Code Example
# Joining a list of words with a comma
print(", ".join(["Apple", "Banana", "Cherry"]))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • In this last example, a list of words is joined with commas. Each comma is like the glue that joins the words together.
  • Just remember: the "glue" comes first, then the .join(), and then the things you want to stick together inside the parentheses.

Test Incomplete

What symbol is used to join words together in Python?

Question #

1/15

Score

0/0 - 0.0 %