You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Strings are one of the most commonly used data types in Python. A string is a sequence of characters enclosed in quotes. Python provides a rich set of methods for working with text — searching, slicing, formatting, and transforming.
# Single quotes
greeting = 'Hello, World!'
# Double quotes
message = "Python is fun!"
# Triple quotes — multi-line strings
paragraph = """This is a
multi-line string that
spans three lines."""
# Triple single quotes also work
another = '''Same thing
with single quotes.'''
print(greeting)
print(paragraph)
Use double quotes when your string contains an apostrophe, and single quotes when it contains double quotes:
sentence = "It's a beautiful day."
html = '<div class="container">Hello</div>'
Every character in a string has a position (index), starting from 0:
word = "Python"
# P y t h o n
# 0 1 2 3 4 5
# -6-5-4-3-2-1
print(word[0]) # P — first character
print(word[5]) # n — last character
print(word[-1]) # n — last character (negative indexing)
print(word[-2]) # o — second-to-last
Extract a portion of a string using [start:stop:step]:
text = "Hello, World!"
print(text[0:5]) # Hello — characters 0 to 4
print(text[7:12]) # World — characters 7 to 11
print(text[:5]) # Hello — from the start
print(text[7:]) # World! — to the end
print(text[::2]) # Hlo ol! — every 2nd character
print(text[::-1]) # !dlroW ,olleH — reversed string
Key rule: The stop index is exclusive — the character at that position is NOT included.
Use len() to get the number of characters:
message = "Hello!"
print(len(message)) # 6
empty = ""
print(len(empty)) # 0
# Concatenation with +
first = "Hello"
second = "World"
combined = first + ", " + second + "!"
print(combined) # Hello, World!
# Repetition with *
line = "-" * 40
print(line) # ----------------------------------------
# You cannot concatenate strings and numbers directly
age = 25
# print("I am " + age) # TypeError!
print("I am " + str(age)) # I am 25
Python strings have many built-in methods. Here are the most important ones:
text = "Hello, World!"
print(text.upper()) # HELLO, WORLD!
print(text.lower()) # hello, world!
print(text.title()) # Hello, World!
print(text.capitalize()) # Hello, world!
print(text.swapcase()) # hELLO, wORLD!
text = "Hello, World! Hello, Python!"
print(text.find("World")) # 7 — index of first occurrence
print(text.find("Java")) # -1 — not found
print(text.index("World")) # 7 — like find() but raises ValueError if not found
print(text.count("Hello")) # 2 — number of occurrences
print(text.startswith("Hello")) # True
print(text.endswith("Python!")) # True
print("World" in text) # True — membership test
messy = " Hello, World! "
print(messy.strip()) # "Hello, World!" — both sides
print(messy.lstrip()) # "Hello, World! " — left only
print(messy.rstrip()) # " Hello, World!" — right only
# Strip specific characters
url = "///path/to/file///"
print(url.strip("/")) # "path/to/file"
text = "Hello, World!"
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.