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.

python
name = "Alice"
city = 'London'

print(name)
print(city)

Output

text
Alice
London

Single and double quotes work the same way, so choose the form that makes the text clearest.

Creating Strings

Single Quotes

python
language = 'Python'

Double Quotes

python
course = "Web Development"

Triple Quotes

Triple quotes are useful for multi-line text.

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

python
text = "Python"

print(text[0])
print(text[3])

Output

text
P
h

Negative indexing counts from the end, so -1 refers to the last character.

python
text = "Python"

print(text[-1])

Output

text
n

String Slicing

Slicing extracts part of a string. The starting index is included, while the ending index is excluded.

python
string[start:end]
python
text = "Programming"

print(text[0:7])
print(text[3:8])

Output

text
Program
gramm

A third slicing value defines the step, allowing characters to be skipped.

python
text = "Python"

print(text[::2])

Output

text
Pto

String Concatenation

Concatenation joins two or more strings. The + operator combines strings into one.

python
first = "Hello"
second = "World"

print(first + " " + second)

Output

text
Hello World

Repeating Strings

The * operator repeats a string multiple times, which is useful for simple patterns or repeated messages.

python
print("Hi! " * 3)

Output

text
Hi! Hi! Hi!

Common String Methods

Convert to Uppercase

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

Convert to Lowercase

python
text = "PYTHON"
print(text.lower())

Capitalize the First Letter

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

Remove Extra Outer Spaces

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

Replace Text

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

Find Text

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

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

python
name = "Emma"
age = 22

print(f"{name} is {age} years old.")

Output

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

python
word = "Python"

for letter in word:
    print(letter)

Output

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

python
text = "Python"

print(len(text))

Output

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

python
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.
Let's learn with DevBrainBox AI