Strings are how we work with text in Python. You can store letters, words, sentences — even full paragraphs. Since text shows up in nearly every program, strings are one of the most important data types you’ll use.
Because strings are made up of characters in a sequence, you can access each part, combine them, pull them apart, or tweak them however you like. Let’s break it down.
Creating Strings
To create a string in Python, just wrap your text in either single quotes (') or double quotes ("). Both work the same — just be consistent within your project. You can also make strings that span multiple lines using triple quotes.
name = "Alice"
message = 'Hello, world!'
multiline = """This
is a string
across lines."""
String Operations
You can do some cool stuff with strings using just a few basic operators. The + sign joins strings together. The * sign repeats a string as many times as you like. This is great for custom messages or formatting your output.
greeting = "Hello, "
name = "Alice"
print(greeting + name) # Hello, Alice
print("-" * 10) # ----------
String Indexing and Slicing
Each letter in a string has a number attached to it — called an index. The first character starts at position 0. You can use square brackets to grab a specific letter or slice out a chunk of the string.
You can also use negative numbers to count from the end, which comes in handy a lot.
word = "Python"
print(word[0]) # 'P'
print(word[-1]) # 'n'
print(word[0:4]) # 'Pyth'
String Methods
Python gives you loads of built-in tools (called methods) to work with strings. You can change the case, clean up spaces, or check how a string starts or ends. These are especially useful when you’re handling user input or preparing data.
text = " Python is Fun! "
print(text.lower()) # ' python is fun! '
print(text.strip()) # 'Python is Fun!'
print(text.startswith("Py")) # False (extra spaces)
Interactive Exercise: Personalized Greeting
Try building your own custom message by combining strings. Change the name or tweak the message to see how it works.
name = "Alice"
# Create a personalized greeting
greeting = "Hello, " + name + "! Welcome to Python."
print(greeting)
+ to join strings together.Interactive Exercise: String Analysis
Let’s explore what a string can tell us. Try checking the length, making everything uppercase, and splitting the sentence into words.
sentence = "Learning Python is fun!"
print("Length:", len(sentence))
print("Uppercase:", sentence.upper())
print("Words:", sentence.split())
len(), .upper(), and .split().
Strings are super flexible — whether you’re formatting names, analyzing sentences, or creating dynamic messages. The more you practice with them, the more confident you’ll feel building programs that people can actually interact with.