CodingNic

Python Foundations

Strings and String Methods

Python Foundations 22 min read

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.

python
name = "Alex"
city = 'Nairobi'
message = "Welcome to Python"

Accessing Characters

Each character in a string has a position called an index.

python
word = "Python"
print(word[0])
print(word[1])

Output:

text
P
y

String Length

Use the len() function to count characters.

python
text = "Python"
print(len(text))

Output:

text
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.

python
name = "alex"
print(name.upper())

lower()

Convert text to lowercase.

python
name = "ALEX"
print(name.lower())

strip()

Remove spaces at the beginning and end.

python
text = "  hello  "
print(text.strip())

replace()

Replace text with new text.

python
message = "I like Java"
print(message.replace("Java", "Python"))

title()

Capitalize the first letter of each word.

python
text = "python for everyone"
print(text.title())

capitalize()

Capitalize only the first letter of the string.

python
text = "python"
print(text.capitalize())

startswith()

Check whether a string starts with specific text.

python
email = "admin@example.com"
print(email.startswith("admin"))

endswith()

Check whether a string ends with specific text.

python
file = "report.pdf"
print(file.endswith(".pdf"))

split()

Split text into a list.

python
text = "red,blue,green"
print(text.split(","))

join()

Join items into a string.

python
colors = ["red", "blue", "green"]
print(" - ".join(colors))

find()

Find the position of text.

python
word = "Python"
print(word.find("t"))

String Concatenation

Combine strings using +.

python
first = "Hello"
second = "World"
print(first + " " + second)

string.format()

Another way to insert values into strings.

python
name = "Maya"
age = 20
print("My name is {} and I am {} years old.".format(name, age))

You can also control positions:

python
print("{1} is learning {0}".format("Python", "Maya"))

f-Strings

A cleaner way to insert variables into text.

python
name = "Maya"
age = 20
print(f"My name is {name} and I am {age} years old.")

Code Along

python
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

  1. What is a string?
  2. What function counts characters?
  3. What does upper() do?
  4. 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.