Python Foundation

Strings in Python

01 · What is a String? Strings · 1 of 9

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.

Python
name = "Kundan"
message = 'Hello Python'
course = "Data Science"

print(name)
print(type(name))
Output
Kundan
<class 'str'>

Strings are one of the most commonly used data types in Python.

02 · Creating Strings Strings · 2 of 9

Creating Strings

You can create strings using single quotes, double quotes, or triple quotes.

Python
# 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)
Output
Hello
Python
This is
a multi-line
string

Triple quotes are useful for multi-line text and documentation.

03 · Indexing Strings · 3 of 9

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).

Python
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
Output
P
y
n
n
o

Trying to access an index that does not exist will raise IndexError.

04 · Slicing Strings · 4 of 9

String Slicing

Slicing allows you to extract a portion (substring) of a string.

Syntax: string[start : stop : step]

Python
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
Output
Python
Programming
Python
Pto rgamn
gnimmargorP nohtyP
05 · String Methods Strings · 5 of 9

Important String Methods

Python provides many built-in methods to work with strings.

Python
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
Output
  HELLO PYTHON  
  hello python  
Hello Python
  Hello World  
['Hello', 'Python']

More Useful Methods

Python
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"]))
Output
7
1
True
True
A-B-C
06 · String Formatting Strings · 6 of 9

String Formatting

There are multiple ways to format strings in Python. The most recommended way is f-strings.

1. f-Strings (Recommended)

Python
name = "Kundan"
age = 25

print(f"My name is {name} and I am {age} years old.")
Output
My name is Kundan and I am 25 years old.

2. format() Method

Python
print("My name is {} and I am {} years old.".format(name, age))

3. Old style (%)

Python
print("My name is %s and I am %d years old." % (name, age))
07 · Immutability Strings · 7 of 9

Strings are Immutable

Once a string is created, you cannot change its characters directly.

Python
text = "Python"
# text[0] = "J"    → This will raise TypeError

If you want to modify a string, you must create a new string.

Python
text = "Python"
new_text = "J" + text[1:]
print(new_text)
Output
Jython

This is why string methods always return a new string instead of modifying the original one.

08 · Escape Characters Strings · 8 of 9

Escape Characters

Escape characters are used to insert special characters into a string.

Python
print("Hello\nWorld")      # New line
print("Hello\tWorld")      # Tab space
print("He said \"Python\" is fun")
print('It\'s a good day')
Output
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
09 · Summary Strings · 9 of 9

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

Home Strings 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 →