Python Strings Explained (Basics and Examples)
Strings are one of the most common data types in Python.
A string is used to store text. In this guide, you will learn what strings are, how to create them, and how to do basic string tasks like indexing, slicing, joining, and changing text.
Quick example
Use this short example to see what a string looks like, how to access characters, and how string methods work.
text = "Python"
print(text)
print(text[0])
print(text[-1])
print(text[0:3])
print(text.upper())
Output:
Python
P
n
Pyt
PYTHON
What a string is
A string is text data in Python.
You write strings inside quotes. Both single quotes and double quotes work.
Examples:
"hello"
'Python'
"123"
Even "123" is still a string because it is inside quotes. It is text, not a number.
If you are new to Python data types, see Python data types overview or what is a string in Python.
How to create strings
The most common way to create a string is to assign text to a variable.
name = "Alice"
language = 'Python'
Single and double quotes
Single quotes are fine for simple text:
message = 'Hello'
Double quotes are useful when the text contains an apostrophe:
message = "It's working"
If you wrote that with single quotes, Python would get confused about where the string ends.
Triple quotes for multi-line text
Use triple quotes when you want a string that spans multiple lines.
text = """Line one
Line two
Line three"""
print(text)
Strings are ordered sequences
A string is an ordered sequence of characters.
That means each character has a position, called an index.
In the string "Python":
Pis at index0yis at index1tis at index2
Python starts counting at 0, not 1.
Negative indexes count from the end:
-1is the last character-2is the second-to-last character
Example:
text = "Python"
print(text[0]) # first character
print(text[1]) # second character
print(text[-1]) # last character
Output:
P
y
n
You access one character by using square brackets after the string.
Indexing and slicing
Indexing
Indexing gets one character from a string.
text = "Python"
print(text[0])
print(text[3])
Output:
P
h
Slicing
Slicing gets part of a string.
The basic format is:
text[start:stop]
Important: the stop position is not included.
text = "Python"
print(text[0:3])
print(text[2:6])
print(text[:4])
print(text[4:])
Output:
Pyt
thon
Pyth
on
A common beginner example is:
text = "Python"
print(text[0:3])
This gives "Pyt" because it starts at index 0 and stops before index 3.
Common string operations
You can do several simple operations with strings.
Join strings with +
Use + to combine strings.
first = "Hello"
second = "World"
result = first + " " + second
print(result)
Output:
Hello World
Repeat strings with *
Use * to repeat a string.
print("ha" * 3)
Output:
hahaha
Get the length with len()
Use len() to count how many characters are in a string.
text = "Python"
print(len(text))
Output:
6
For more on this, see Python len() function explained.
Check for text with in
Use in to check whether one string appears inside another.
text = "Python programming"
print("Python" in text)
print("java" in text)
Output:
True
False
Useful string methods
String methods are built-in tools that help you work with text.
lower()
lower() makes all letters lowercase.
text = "PyThOn"
print(text.lower())
Output:
python
See Python string lower() method.
upper()
upper() makes all letters uppercase.
text = "Python"
print(text.upper())
Output:
PYTHON
See Python string upper() method.
strip()
strip() removes whitespace from the start and end of a string.
text = " hello "
print(text.strip())
Output:
hello
See Python string strip() method.
replace()
replace() changes one part of the text into another.
text = "I like cats"
print(text.replace("cats", "dogs"))
Output:
I like dogs
See Python string replace() method.
split()
split() turns a string into a list.
text = "red,green,blue"
colors = text.split(",")
print(colors)
Output:
['red', 'green', 'blue']
See Python string split() method or how to split a string in Python.
join()
join() joins multiple strings into one string.
words = ["Python", "is", "fun"]
result = " ".join(words)
print(result)
Output:
Python is fun
See Python string join() method or how to join strings in Python.
Strings are immutable
Strings are immutable.
This means you cannot change one character directly.
For example, this causes an error:
text = "hello"
text[0] = "H"
Python does not allow direct character assignment in strings.
Instead, create a new string:
text = "hello"
text = "H" + text[1:]
print(text)
Output:
Hello
This is an important beginner idea. When a string changes, Python creates a new string instead of changing the old one in place.
Comparing strings
You can compare strings with ==.
print("Python" == "Python")
print("Python" == "python")
Output:
True
False
String comparisons are case-sensitive. That means uppercase and lowercase letters matter.
If you want a case-insensitive comparison, convert both strings first:
a = "Python"
b = "python"
print(a.lower() == b.lower())
Output:
True
When beginners use strings
Beginners work with strings all the time, often without noticing it.
input() returns a string
Anything entered with input() comes in as a string.
age = input("Enter your age: ")
print(age)
print(type(age))
If you need a number, convert it with int() or float().
age = input("Enter your age: ")
age = int(age)
print(age + 1)
File content is often read as strings
When you read text from a file, Python usually gives you strings.
Mixing strings and numbers causes errors
A common beginner mistake is trying to add text and numbers directly.
age = 20
print("Age: " + age)
This fails because "Age: " is a string and age is an integer.
Fix it by converting the number:
age = 20
print("Age: " + str(age))
If you get this error, see TypeError: can only concatenate str not int to str.
Common mistakes
Here are some common problems beginners have with strings:
- Using quotes incorrectly when creating text
- Trying to change a string character directly
- Mixing strings and numbers with
+ - Using the wrong index position
- Forgetting that
input()returns a string
These quick checks can help when debugging:
print(text)
print(type(text))
print(len(text))
print(text[0])
print(text.lower())
What these do:
print(text)shows the actual valueprint(type(text))shows whether it is a stringprint(len(text))shows the number of charactersprint(text[0])checks the first characterprint(text.lower())helps test case-insensitive text handling
FAQ
What is a string in Python?
A string is a sequence of characters used to store text.
How do I create a string in Python?
Put text inside single quotes, double quotes, or triple quotes.
Can I change one character in a string?
No. Strings are immutable, so you must create a new string.
What is the difference between indexing and slicing?
Indexing gets one character. Slicing gets part of the string.
Why does adding a string and a number fail?
They are different data types. Convert the number with str() or the string with int() when appropriate.