Python Strings
Create, format, search, slice, and transform text.
Text is one of the most common types of data used in programming. Whether you are storing a person's name, displaying a welcome message, saving an email address, or processing user input, you are working with strings.
Python provides powerful and easy-to-use tools for handling strings. You can create them, access individual characters, combine them, search within them, and produce modified versions with built-in methods.
In this lesson, you will learn what strings are, how to work with them, and some of the most useful string operations in Python.
What is a String?
A string is a sequence of characters enclosed in single or double quotation marks. A string can contain letters, numbers, spaces, symbols, and special characters.
name = "Alice"
city = 'London'
print(name)
print(city)Output
Alice
LondonSingle and double quotes work the same way, so choose the form that makes the text clearest.
Creating Strings
Single Quotes
language = 'Python'Double Quotes
course = "Web Development"Triple Quotes
Triple quotes are useful for multi-line text.
message = """Welcome to DevBrainBox.
Learn Python step by step.
Happy Coding!"""
print(message)Accessing Characters
Each character has a position called an index. Python starts counting from zero. In the word Python, P is at index 0 and n is at index 5.
text = "Python"
print(text[0])
print(text[3])Output
P
hNegative indexing counts from the end, so -1 refers to the last character.
text = "Python"
print(text[-1])Output
nString Slicing
Slicing extracts part of a string. The starting index is included, while the ending index is excluded.
string[start:end]text = "Programming"
print(text[0:7])
print(text[3:8])Output
Program
grammA third slicing value defines the step, allowing characters to be skipped.
text = "Python"
print(text[::2])Output
PtoString Concatenation
Concatenation joins two or more strings. The + operator combines strings into one.
first = "Hello"
second = "World"
print(first + " " + second)Output
Hello WorldRepeating Strings
The * operator repeats a string multiple times, which is useful for simple patterns or repeated messages.
print("Hi! " * 3)Output
Hi! Hi! Hi!Common String Methods
Convert to Uppercase
text = "python"
print(text.upper())Convert to Lowercase
text = "PYTHON"
print(text.lower())Capitalize the First Letter
text = "python programming"
print(text.capitalize())Remove Extra Outer Spaces
text = " Python "
print(text.strip())Replace Text
text = "I like Java"
print(text.replace("Java", "Python"))Find Text
text = "Welcome to Python"
print(text.find("Python"))These methods make it easy to normalize, clean, search, and transform text.
Checking String Content
Python provides methods that examine a string's contents and return True or False.
text = "Python123"
print(text.isalpha())
print(text.isdigit())
print(text.isalnum())Using f-Strings
An f-string is the recommended modern approach for inserting variables and expressions into text.
name = "Emma"
age = 22
print(f"{name} is {age} years old.")Output
Emma is 22 years old.Looping Through a String
Because a string is a sequence of characters, a for loop can process it one character at a time.
word = "Python"
for letter in word:
print(letter)Output
P
y
t
h
o
nString Length
The len() function returns the number of characters in a string. This is helpful when validating input or processing text.
text = "Python"
print(len(text))Output
6String Immutability
Strings are immutable, meaning their characters cannot be changed after creation. String methods return new strings instead of modifying the original object. A variable can then be assigned to the new result.
text = "Python"
text = text.replace("Python", "Java")
print(text)Key Takeaways
- A string is a sequence of characters enclosed in single, double, or triple quotes.
- Strings can store letters, numbers, spaces, and symbols.
- Characters are accessed using indexes that begin at zero.
- String slicing extracts selected parts of a string.
- The + operator joins strings, while the * operator repeats them.
- Methods such as upper(), lower(), strip(), replace(), and find() simplify text processing.
- F-strings provide a clean and readable way to format text with variables.
- The len() function returns the length of a string.
- Strings are immutable and cannot be changed in place.
- Strings are essential for building real-world Python applications.