Strings and String Methods
Strings and String Methods
Strings are one of the most common data types in Python. A string is any text wrapped in quotes. You will use strings when working with names, messages, emails, file paths, and user input.
What Is a String?
Strings can use single quotes or double quotes.
name = "Alex"
city = 'Nairobi'
message = "Welcome to Python"
Accessing Characters
Each character in a string has a position called an index.
word = "Python"
print(word[0])
print(word[1])
Output:
P
y
String Length
Use the len() function to count characters.
text = "Python"
print(len(text))
Output:
6
Useful String Methods
Python provides many built-in string methods that help you clean, search, transform, and format text efficiently.
upper()
Convert text to uppercase.
name = "alex"
print(name.upper())
lower()
Convert text to lowercase.
name = "ALEX"
print(name.lower())
strip()
Remove spaces at the beginning and end.
text = " hello "
print(text.strip())
replace()
Replace text with new text.
message = "I like Java"
print(message.replace("Java", "Python"))
title()
Capitalize the first letter of each word.
text = "python for everyone"
print(text.title())
capitalize()
Capitalize only the first letter of the string.
text = "python"
print(text.capitalize())
startswith()
Check whether a string starts with specific text.
email = "admin@example.com"
print(email.startswith("admin"))
endswith()
Check whether a string ends with specific text.
file = "report.pdf"
print(file.endswith(".pdf"))
split()
Split text into a list.
text = "red,blue,green"
print(text.split(","))
join()
Join items into a string.
colors = ["red", "blue", "green"]
print(" - ".join(colors))
find()
Find the position of text.
word = "Python"
print(word.find("t"))
String Concatenation
Combine strings using +.
first = "Hello"
second = "World"
print(first + " " + second)
string.format()
Another way to insert values into strings.
name = "Maya"
age = 20
print("My name is {} and I am {} years old.".format(name, age))
You can also control positions:
print("{1} is learning {0}".format("Python", "Maya"))
f-Strings
A cleaner way to insert variables into text.
name = "Maya"
age = 20
print(f"My name is {name} and I am {age} years old.")
Code Along
name = "sam"
print(name.upper())
print(name.capitalize())
print(len(name))
Mini Challenge
Create a variable with your full name and:
- Print it in uppercase
- Print it in lowercase
- Count the letters
- Replace your first name with another name
Real World Use Case
Web apps use string methods to clean form data, validate emails, format messages, and personalize user experiences.
Quiz
- What is a string?
- What function counts characters?
- What does
upper()do? - What is the difference between concatenation and f-strings?
Assignment
Create a program that asks for a user’s name and prints a personalized welcome message in three different formats.
Summary
You learned what strings are, how indexing works, how to use common string methods, and how to format text professionally in Python.