Mathew K Analytics

Lesson 10 · Python for Data Science

Master Python Strings: Essential Operations and Methods for Data Science

Strings are how we work with text in programming. We will learn how to create, change, and use strings step by step. Get ready to unlock lots of creative…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb
 

Welcome to Python Strings!#

Strings are how we work with text in programming. We will learn how to create, change, and use strings step by step. Get ready to unlock lots of creative power.

# Let us make a simple string variable
greeting = 'Hello, world!'
print(greeting)
Hello, world!

What are Strings?#

Strings are just sequences of characters surrounded by quotes. You can use single or double quotes: 'hello' or "hello". They are used for anything that is text, like names or messages.

# Using double quotes instead of single quotes
city = "London"
print(city)
London
# What if your text has an apostrophe?
food = 'It is Jenny\'s cake.'
print(food)
It is Jenny's cake.
# Multi-line strings can use triple quotes
story = '''Once upon a time,
there was a coder.
They loved Python.'''
print(story)
Once upon a time,
there was a coder.
They loved Python.
# Let us check what type our variable is
print(type(greeting))
<class 'str'>

Working With Characters#

Strings are like letter trains. Each letter has a position called an index. You can use brackets to select any character. Let us find out how!

# The first character has index 0
name = 'Python'
print('First letter:', name[0])
First letter: P
# Negative indexes count from the end
print('Last letter:', name[-1])
Last letter: n
# Slicing means getting part of a string
language = 'javascript'
print(language[0:4])  # Get the first four letters
java
# Strings are immutable, which means you cannot change them in place
# You must make a new string if you want something different
pet = 'cat'
# pet[0] = 'b'  # This line would cause an error
animal = 'b' + pet[1:]
print(animal)
bat

String Methods#

Methods are actions you can do with strings, like changing their case or finding words. Methods use a dot after the string, like .upper() to make everything uppercase.

# .upper() turns all letters into uppercase
phrase = 'good morning'
print(phrase.upper())
GOOD MORNING
# .replace() swaps old text for something new
cheer = 'hip hip hooray!'
new_cheer = cheer.replace('hip', 'yay')
print(new_cheer)
yay yay hooray!
# .find() searches for where a string appears
sentence = 'Where is the cat?'
position = sentence.find('cat')
print('Cat starts at:', position)
Cat starts at: 13
# Getting user input and working with it
user_text = input('Type something fun: ')
upper_text = user_text.upper()
print('You in caps:', upper_text)
You in caps: PYTHON IS COOL!
 
# Practice: Ask the user for their name, then greet them
name = input('What is your name? ')
print('Nice to meet you, ' + name + '!')
Nice to meet you, Sam!
 
# Mini-Project Part 1: Counting words in a sentence
text = input('Give me a sentence: ')
words = text.split()
count = len(words)
print('Your sentence has ' + str(count) + ' words.')
Your sentence has 6 words.
 
# Mini-Project Part 2: Make initials from a full name
full_name = input('Enter your full name: ')
parts = full_name.strip().split()
initials = ''
for part in parts:
    initials += part[0].upper()
print('Your initials are: ' + initials)
Your initials are: TAS
 
# Helpful: Stripping extra spaces from text
raw = '  too much space!   '
fixed = raw.strip()
print('Before:', '[' + raw + ']')
print('After:', '[' + fixed + ']')
Before: [  too much space!   ]
After: [too much space!]
# Common mistake example: mixing numbers and strings
weight = 2
message = 'Your order weighs ' + str(weight) + ' pounds.'
print(message)
Your order weighs 2 pounds.

Quick Challenge!#

Make a string with your favorite food and change it to all uppercase letters. Print out the original and the all-caps version.

Recap: What Have You Learned?#

  • How to make and use strings
  • Picking, slicing, and changing strings
  • Using methods for transforming text
  • Combining strings, numbers, and input
  • Real mini-projects and fun practice ideas

Strings show up everywhere. You now have the keys to use text like a pro!

Thanks for Learning Strings with Us!#

If you found this lesson helpful:

  • Like the video
  • Leave a comment about your favorite string method
  • Subscribe for more coding lessons
  • Share this tutorial with a friend who wants to learn Python

Happy coding!

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.