Python Basics
Python Strings
A string is a sequence of characters used to represent text in Python, and it's one of the data types you'll use the most.
Creating Strings with Quotes
You can create a string using single quotes or double quotes — Python treats them the same way. Use whichever is convenient, especially when your text itself contains one type of quote.
greeting = 'Hello there'
name = "Aisha's friend"
print(greeting, name)String Indexing
Each character in a string has a position, called an index, starting from 0. You can access a single character using square brackets, and negative indexes count backward from the end.
word = "Python"
print(word[0]) # P
print(word[5]) # n
print(word[-1]) # n (last character)
print(word[0:3]) # Pyt (slicing)String Concatenation
You can join, or "concatenate", strings together using the + operator. Remember that + only works between strings, so numbers must be converted first.
first = "Learn"
second = "Python"
combined = first + " " + second
print(combined)f-strings — Formatted Strings
f-strings are the modern, preferred way to build strings that include variable values. Add an f before the opening quote, then place variables directly inside curly braces.
name = "Sanya"
age = 23
message = f"{name} is {age} years old"
print(message)Multiline Strings
Wrap text in triple quotes to create a string that spans multiple lines, which is useful for paragraphs or docstrings.
bio = """Hi, I'm learning Python.
It is my first programming language.
I'm excited to build real projects!"""
print(bio)Prefer f-strings over old-style + concatenation — they are faster to write and much easier to read.
