Part 3 · Variables and types — chapter 4 of 6 · chapter 14 of 50 · 7 min read

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
P0-6y1-5t2-4h3-3o4-2n5-1[0:3]
text[0] is P, text[-1] is n, and text[0:3] is Pyt (stop index 3 excluded).

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":

  • P is at index 0
  • y is at index 1
  • t is at index 2

Python starts counting at 0, not 1.

Negative indexes count from the end:

  • -1 is the last character
  • -2 is 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 value
  • print(type(text)) shows whether it is a string
  • print(len(text)) shows the number of characters
  • print(text[0]) checks the first character
  • print(text.lower()) helps test case-insensitive text handling

✍️ Try it yourself

Start with the string text = "Programming". Print its first three characters using a slice, and print the whole word in uppercase using a string method.

Show answer
text = "Programming"

print(text[0:3])
print(text.upper())
# Output:
# Pro
# PROGRAMMING

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.

See also #

Press Esc to close