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 London
Single 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 h
Negative indexing counts from the end, so -1 refers to the last character.
text = "Python"
print(text[-1])Output
n
String 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 gramm
A third slicing value defines the step, allowing characters to be skipped.
text = "Python"
print(text[::2])Output
Pto
String Concatenation
Concatenation joins two or more strings. The + operator combines strings into one.
first = "Hello"
second = "World"
print(first + " " + second)Output
Hello World
Repeating 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 n
String 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
6
String 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)