Learning Guides
Python Strings: Complete Guide with Examples
Quick answer: Learn Python string basics including indexing, slicing, common string methods, f-strings, and immutability, with practical examples.
What is a String?
A string is an ordered, immutable sequence of characters, created using single, double or triple quotes.
name = 'Fireblaze'
course = "Data Science"
description = '''A multi-line
string spanning
several lines'''
Indexing and Slicing
text = "Data Science"
print(text[0]) # D
print(text[-1]) # e
print(text[0:4]) # Data
print(text[::-1]) # ecneicS ataD, reversed
Strings are Immutable
text = "Hello"
text[0] = "J" # TypeError: 'str' object does not support item assignment
text = "Jello" # this creates a NEW string, it does not modify the old one
Common String Methods
text = " Data Science "
print(text.strip()) # "Data Science", removes leading/trailing whitespace
print(text.upper()) # " DATA SCIENCE "
print(text.lower()) # " data science "
print(text.strip().replace("Data", "AI")) # "AI Science"
print(text.strip().split(" ")) # ['Data', 'Science']
Checking String Content
email = "student@fireblaze.in"
print(email.startswith("student")) # True
print(email.endswith(".in")) # True
print("@" in email) # True
print(email.isdigit()) # False
F-Strings for Formatting
F-strings, introduced in Python 3.6, are the modern, readable way to embed variables directly inside a string.
name = "Priya"
score = 87.456
print(f"{name} scored {score:.1f}%") # Priya scored 87.5%
Joining and Splitting
words = ['Data', 'Science', 'Course']
sentence = " ".join(words) # "Data Science Course"
parts = sentence.split(" ") # ['Data', 'Science', 'Course']
Common Interview Questions
Why are strings immutable in Python?
Immutability makes strings safe to share across a program without risk of unexpected modification, and allows them to be used as dictionary keys since their value cannot change.
What is the difference between str.strip() and str.replace()?
strip() removes leading and trailing whitespace by default, or specified characters if given an argument. replace() substitutes every occurrence of a specified substring with another string, anywhere in the string.
What is the advantage of f-strings over older string formatting methods?
F-strings are more concise and readable, and allow direct embedding of expressions inside the string itself, evaluated at runtime.
FAQ
Frequently Asked Questions
Why are strings immutable in Python?
Immutability makes strings safe to share across a program without risk of unexpected modification, and lets them be used as dictionary keys since their value can never change.
What is an f-string in Python?
A formatted string literal, written as f'...', that lets you embed variables and expressions directly inside a string using curly braces.
How do you reverse a string in Python?
Using slicing with a step of -1, such as text[::-1].
What is the difference between split() and join()?
split() breaks a string into a list of substrings based on a separator. join() does the reverse, combining a list of strings into one string using a specified separator.
Keep Reading
Related Articles
Learning Guides
INNER JOIN in SQL with Examples: Complete Beginner's Guide
Learn INNER JOIN in SQL with practical examples, syntax, interview questions, and real-world use cases. A complete beginner-friendly SQL JOI
Learning Guides
OUTER JOIN in SQL: Complete Guide with Examples for Beginners
Master OUTER JOIN in SQL with practical examples and real-world scenarios. Learn LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, syntax, use cases,
Learning Guides
Multi-Row Functions in SQL: Complete Guide with Examples
Learn multi-row (aggregate) functions in SQL including SUM, AVG, COUNT, MIN, MAX, GROUP BY and HAVING, with practical examples, NULL handlin