Skip to content

Python! Working with Strings | Lectures 11

Lecture 11 Working with Strings

Lets Start Python Programming For Beginners to advanced. Working with Strings, Lectures 11

🐍 Lecture 11: Working with Strings

🎯 Goal

By the end of this lecture, you will:

  • Understand how to slice , format , and search through strings.
  • Be able to use common string methods like .upper(), .lower(), .replace(), and .split().
  • Practice building fun text-based games or tools like a Mad Libs GeneratorΫ”

🧠 What Are Strings?

A string is any text in Python β€” whether it’s a single letter, a word, a sentence, or even a whole paragraph.

You already know how to create strings using quotes:

message = "Hello, world!"
name = 'Alice'

Now let’s learn how to work with strings more powerfully!

πŸ”€ String Indexing – Finding Letters

Each character in a string has a position (called an index ), starting from 0.

Example:

text = "Python"
print(text[0])  # Output: P
print(text[3])  # Output: h

You can also go from the end:

print(text[-1])  # Output: n

βœ‚οΈ String Slicing – Cutting Out Parts

Slicing lets you get part of a string using [start:end].

text = "Hello, world!"
print(text[0:5])   # Output: Hello
print(text[7:12])  # Output: world

Leave out start or end to go from beginning or to end:

print(text[:5])    # Hello
print(text[7:])    # world!

πŸ” Common String Methods

Strings come with built-in tools called methods . Here are some of the most useful ones:

.upper() and .lower() – Make Text Big or Small
name = "Alice"
print(name.upper())  # ALICE
print(name.lower())  # alice

.replace() – Swap Words

story = "I love apples."
new_story = story.replace("apples", "bananas")
print(new_story)  # I love bananas.

.split() – Break Into Words

words = "apple banana cherry"
parts = words.split()
print(parts)  # ['apple', 'banana', 'cherry']

.strip() – Remove Extra Spaces

text = "   Hello!   "
print(text.strip())  # Hello!

.find() – Search for Text

sentence = "Where is the treasure?"
print(sentence.find("treasure"))  # Position 11
Lecture 11 Working with Strings
Lecture 11 Working with Strings

πŸ§ͺ Try It Yourself!

Write code that:

  1. Asks the user for their name.
  2. Converts it to uppercase.
  3. Replaces all 'A' letters with '@'.
  4. Prints the result.

πŸ’‘ Sample Code:

name = input("Enter your name: ")
modified = name.upper().replace('A', '@')
print("Modified name:", modified)

πŸ‘‰ Example Run:

Enter your name: Alice
Modified name: @LICE

🎨 Mini Project: Mad Libs Generator

Let’s build a fun game where the user fills in blanks and we make a silly story!

print("Welcome to Mad Libs!")
noun = input("Enter a noun: ")
verb = input("Enter a verb: ")
adjective = input("Enter an adjective: ")

print(f"The {adjective} {noun} loves to {verb} every morning!")

πŸ‘‰ Example Run:

Enter a noun: cat
Enter a verb: dance
Enter an adjective: silly
The silly cat loves to dance every morning!

πŸ§’ Kids Corner

🧠 Imagine you have a robot friend who loves telling stories but always forgets parts.

You give it clues like:

  • β€œPut β€˜dog’ here!”
  • β€œSay β€˜jump’ instead of β€˜run’!”

That’s what we’re doing with strings β€” helping our robot tell better stories!

πŸ€– Robot says:

Once upon a time... wait, what was the name again?

πŸš€ Challenge (Optional)

Build a secret message encoder that:

  1. Takes a normal message from the user.
  2. Encodes it by reversing the string and making it uppercase.

Example:

message = input("Enter your message: ")
encoded = message[::-1].upper()
print("Encoded message:", encoded)

πŸ‘‰ If the user types "hello", it becomes "OLLEH".

Try decoding one another’s messages!

πŸ“Œ Summary

  • Strings are text in Python, created with quotes.
  • You can slice them to get parts of the text.
  • Use string methods like .upper(), .lower(), .replace(), and .split() to change or analyze text.
  • Strings are powerful for creating games, stories, and interactive programs.
Stay Updated

If you found this information useful, don’t forget to bookmark this page and Share and leave your feedback in the comment section below.

Python Programming For Beginners To Advanced

Python Programming For Beginners! Lecture 1

Python Programming! Variables and Data Types | Lectures 2

Python Programming! Input And Output | Lectures 3

Python Programming! Operators in Python Lectures 4

Python Programming! Conditional Statements Lectures 5

Python! Loops For and While | Lectures 6

Python! Building A Mini Game | Lectures 7

Python! Functions | Lectures 8

Python! Storing Multiple Values | Lectures 9 

Python! Tuples Sets And Dictionaries | Lectures 10

Python! Working with Strings | Lectures 11

Najeeb Alam

Najeeb Alam

Technical writer specializes in developer, Blogging and Online Journalism. I have been working in this field for the last 20 years.

Leave a Reply

Your email address will not be published. Required fields are marked *