Strings in Python
What is a String?
A string is a sequence of characters used to store text data.
In Python, anything written inside quotes is treated as a string.
name = "Kundan"
message = 'Hello Python'
course = "Data Science"
print(name)
print(type(name))
Kundan <class 'str'>
Strings are one of the most commonly used data types in Python.
Creating Strings
You can create strings using single quotes, double quotes, or triple quotes.
# Single quotes
a = 'Hello'
# Double quotes
b = "Python"
# Triple quotes (multi-line)
c = """This is
a multi-line
string"""
print(a)
print(b)
print(c)
Hello Python This is a multi-line string
Triple quotes are useful for multi-line text and documentation.
String Indexing
Each character in a string has a position called an index.
Indexing starts from 0 (left to right). Negative indexing starts from -1 (right to left).
text = "Python"
print(text[0]) # First character
print(text[1]) # Second character
print(text[5]) # Last character
print(text[-1]) # Last character (negative)
print(text[-2]) # Second last character
P y n n o
Trying to access an index that does not exist will raise IndexError.
String Slicing
Slicing allows you to extract a portion (substring) of a string.
Syntax: string[start : stop : step]
text = "Python Programming"
print(text[0:6]) # From index 0 to 5
print(text[7:]) # From index 7 to end
print(text[:6]) # From start to index 5
print(text[::2]) # Every 2nd character
print(text[::-1]) # Reverse the string
Python Programming Python Pto rgamn gnimmargorP nohtyP
Important String Methods
Python provides many built-in methods to work with strings.
text = " Hello Python "
print(text.upper())
print(text.lower())
print(text.strip()) # Removes spaces from both sides
print(text.replace("Python", "World"))
print(text.strip().split()) # Split into list
HELLO PYTHON hello python Hello Python Hello World ['Hello', 'Python']
More Useful Methods
msg = "Python is powerful"
print(msg.find("is")) # Returns index
print(msg.count("p")) # Count occurrences
print(msg.startswith("Python"))
print(msg.endswith("full"))
print("-".join(["A", "B", "C"]))
7 1 True True A-B-C
String Formatting
There are multiple ways to format strings in Python. The most recommended way is f-strings.
1. f-Strings (Recommended)
name = "Kundan"
age = 25
print(f"My name is {name} and I am {age} years old.")
My name is Kundan and I am 25 years old.
2. format() Method
print("My name is {} and I am {} years old.".format(name, age))
3. Old style (%)
print("My name is %s and I am %d years old." % (name, age))
Strings are Immutable
Once a string is created, you cannot change its characters directly.
text = "Python"
# text[0] = "J" → This will raise TypeError
If you want to modify a string, you must create a new string.
text = "Python"
new_text = "J" + text[1:]
print(new_text)
Jython
This is why string methods always return a new string instead of modifying the original one.
Escape Characters
Escape characters are used to insert special characters into a string.
print("Hello\nWorld") # New line
print("Hello\tWorld") # Tab space
print("He said \"Python\" is fun")
print('It\'s a good day')
Hello World Hello World He said "Python" is fun It's a good day
Common Escape Characters
\n→ New line\t→ Tab\\→ Backslash\'→ Single quote\"→ Double quote
Summary – Strings in Python
- A string is a sequence of characters.
- Created using single, double, or triple quotes.
- Supports indexing and slicing.
- Strings are immutable.
- Useful methods:
upper(),lower(),strip(),replace(),split(),find(),join() - Best way to format strings → f-strings
Next Topic → Lists in Python
Strings in Python
Learn how to create, index, slice, format and manipulate text data.
Select any topic from the left sidebar to begin.
Start Learning →
