- Home
- /
- Python Operation on Strings
Python Tutorial
Introduction
Python Data Types & Oper...
Python Strings
Python Lists
Python Tuples
Python Sets
Python Dictionaries
Conditional Statements
Python Loops
Python Functions
Python Modules
Python OOPS
Advanced Topics
File Handling
- Home
- /
- Python Operation on Strings
Operations on Strings
Strings aren’t just static pieces of text—they can be analyzed, broken down, looped over, and manipulated in many ways. Let’s explore some of the most useful string operations in Python.
Finding the Length of a String
To count how many characters a string has (including spaces and symbols), use the len()
function.
subject = "Biology"
length = len(subject)
print("The word Biology has", length, "characters.")
Output:
The word Biology has 7 characters.
Strings as Arrays
Since strings are sequences of characters, each character has an index (starting from 0). You can access individual characters or parts of the string using indexing and slicing.
book = "Notebook"
print(book[0]) # First character
print(book[4]) # Fifth character
Output:
N
b
String Slicing
You can extract a portion of a string using slicing. This uses the format:string[start_index:end_index]
(end index is excluded)
book = "Notebook"
print(book[:4]) # From start to index 3
print(book[4:]) # From index 4 to end
print(book[2:6]) # From index 2 to 5
print(book[-4:]) # Last 4 characters using negative indexing
Output:
Note
book
tebo
book
Slicing helps when you need to extract specific data like filenames, dates, or IDs from strings.
Looping Through a String
You can loop through every character in a string using a for
loop—just like you’d loop through a list.
language = "Python"
for letter in language:
print(letter)
Output:
P
y
t
h
o
n
This is useful for checking, modifying, or counting specific characters in a string.
Summary
Use
len()
to count the number of characters in a string.Strings can be accessed by index like arrays.
Use slicing to extract portions of strings:
text[start:end]
.Strings are iterable—loop through them to access each character.