ChatGPT Share
Can you Create easy & detailed notes for Python Strings
Slicing Strings Modify Strings Concatenate Strings Format
Strings Escape character string methods Which cover
📘
these all topics ,
Python Strings – Easy & Detailed Notes
1. Python Strings
What is a String?
A string is a sequence of characters enclosed inside:
Single Quotes ' '
Double Quotes " "
Triple Quotes ''' ''' or """ """
Examples
Python Run
name = "Rahul"
city = 'Delhi'
course = """Python Programming"""
Output:
Rahul
Delhi
Python Programming
Why are Strings Important?
Strings are used to store:
Names
Addresses
Emails
Passwords
Messages
Sentences
File names
Example
Python Run
username = "Admin"
password = "abc123"
email = "abc@[Link]"
String Characteristics
Ordered
Immutable (Cannot be changed directly)
Indexed
Allows duplicate characters
Example
Python Run
text = "Python"
Index Position
P y t h o n
0 1 2 3 4 5
Negative Index
-6 -5 -4 -3 -2 -1
Accessing Characters
Python Run
text = "Python"
print(text[0])
print(text[2])
print(text[-1])
Output
P
t
n
Length of String
Use len()
Python Run
text = "Python"
print(len(text))
Output
Loop Through String
Python Run
text = "Python"
for i in text:
print(i)
Output
P
y
t
h
o
n
Check String
Use in
Python Run
text = "Python Programming"
print("Python" in text)
Output
True
Check Not Present
Python Run
text = "Python"
print("Java" not in text)
Output
True
2. Slicing Strings
Syntax
Python Run
string[start:end]
The end position is not included.
Example
Python Run
text = "Python"
print(text[0:4])
Output
Pyth
Slice From Beginning
Python Run
text = "Python"
print(text[:4])
Output
Pyth
Slice To End
Python Run
text = "Python"
print(text[2:])
Output
thon
Negative Slicing
Python Run
text = "Python"
print(text[-4:-1])
Output
tho
Step Value
Syntax
Python Run
string[start:end:step]
Example
Python Run
text = "Python"
print(text[0:6:2])
Output
Pto
Reverse String
Python Run
text = "Python"
print(text[::-1])
Output
nohtyP
3. Modify Strings
Strings are immutable.
Instead of changing the original string, Python creates a new one.
Upper Case
Python Run
text = "python"
print([Link]())
Output
PYTHON
Lower Case
Python Run
text = "PYTHON"
print([Link]())
Output
python
Remove Spaces
Python Run
text = " Python "
print([Link]())
Output
Python
Replace
Python Run
text = "Hello World"
print([Link]("World", "Python"))
Output
Hello Python
Split String
Python Run
text = "Apple,Banana,Mango"
print([Link](","))
Output
['Apple', 'Banana', 'Mango']
Capitalize
Python Run
text = "python programming"
print([Link]())
Output
Python programming
Title
Python Run
text = "python programming"
print([Link]())
Output
Python Programming
4. Concatenate Strings
Joining two or more strings.
Using +
Python Run
first = "Python"
second = "Programming"
print(first + second)
Output
PythonProgramming
Add Space
Python Run
print(first + " " + second)
Output
Python Programming
Using +=
Python Run
text = "Hello"
text += " World"
print(text)
Output
Hello World
Using join()
Python Run
words = ["I", "Love", "Python"]
print(" ".join(words))
Output
I Love Python
5. Format Strings
Formatting inserts variables inside a string.
Old Method
Python Run
name = "Rahul"
print("Hello %s" % name)
Output
Hello Rahul
format()
Python Run
name = "Rahul"
age = 22
print("My name is {} and I am {} years old.".format(name, age))
Output
My name is Rahul and I am 22 years old.
Index Formatting
Python Run
print("{1} {0}".format("Python", "Learn"))
Output
Learn Python
f-Strings (Recommended)
Python 3.6+
Python Run
name = "Rahul"
age = 22
print(f"My name is {name}.")
print(f"I am {age} years old.")
Output
My name is Rahul.
I am 22 years old.
Expression in f-String
Python Run
price = 100
qty = 5
print(f"Total = {price * qty}")
Output
Total = 500
6. Escape Characters
Used when special characters need to appear inside strings.
Single Quote
Python Run
print('It\'s Python')
Output
It's Python
Double Quote
Python Run
print("He said \"Hello\"")
Output
He said "Hello"
Backslash
Python Run
print("C:\\Users\\Admin")
Output
C:\Users\Admin
New Line
Python Run
print("Hello\nWorld")
Output
Hello
World
Tab Space
Python Run
print("Python\tJava")
Output
Python Java
Carriage Return
Python Run
print("Hello\rWorld")
Output (terminal-dependent)
World
Backspace
Python Run
print("Python\b")
Deletes one character before the cursor (behavior depends on the output environment).
Common Escape Characters
Escape Meaning
\' Single Quote
\" Double Quote
\\ Backslash
\n New Line
\t Tab
\r Carriage Return
\b Backspace
7. Python String Methods
1. upper()
Python Run
text = "python"
print([Link]())
Output
PYTHON
2. lower()
Python Run
print("PYTHON".lower())
Output
python
3. title()
Python Run
print("python programming".title())
Output
Python Programming
4. capitalize()
Python Run
print("python".capitalize())
Output
Python
5. swapcase()
Python Run
print("PyThOn".swapcase())
Output
pYtHoN
6. strip()
Python Run
print(" Python ".strip())
Output
Python
7. lstrip()
Python Run
print(" Python".lstrip())
8. rstrip()
Python Run
print("Python ".rstrip())
9. replace()
Python Run
print("Hello".replace("H", "Y"))
Output
Yello
10. split()
Python Run
print("A,B,C".split(","))
Output
['A', 'B', 'C']
11. join()
Python Run
print("-".join(["A", "B", "C"]))
Output
A-B-C
12. find()
Python Run
text = "Python"
print([Link]("t"))
Output
Returns -1 if not found.
13. index()
Python Run
print("Python".index("t"))
Raises an error if the substring is not found.
14. startswith()
Python Run
print("Python".startswith("Py"))
Output
True
15. endswith()
Python Run
print("Python".endswith("on"))
Output
True
16. count()
Python Run
print("banana".count("a"))
Output
17. isalpha()
Python Run
print("Python".isalpha())
Output
True
18. isdigit()
Python Run
print("12345".isdigit())
Output
True
19. isalnum()
Python Run
print("Python123".isalnum())
Output
True
20. isspace()
Python Run
print(" ".isspace())
Output
True
21. center()
Python Run
print("Python".center(12, "*"))
Output
***Python***
22. zfill()
Python Run
print("45".zfill(5))
Output
00045
Quick Revision Table
Topic Purpose Example
String Store text "Python"
Indexing Access characters text[0]
Slicing Extract part text[1:4]
Reverse Reverse string text[::-1]
upper() Uppercase "abc".upper()
lower() Lowercase "ABC".lower()
strip() Remove spaces " abc ".strip()
replace() Replace text "Hi".replace("H","B")
split() Convert to list "A,B".split(",")
join() Join strings ",".join(list)
Topic Purpose Example
Concatenate Combine strings a + b
f-string Format text f"{name}"
Escape Characters Special formatting \n , \t , \\
len() Length len(text)
find() Find position [Link]("a")
startswith() Check prefix [Link]("Py")
endswith() Check suffix [Link]("on")
Exam Tips
Remember that strings are immutable—methods like replace() , upper() , and
strip() return a new string instead of modifying the original.
Be comfortable with indexing ( text[0] , text[-1] ) and slicing
( text[start:end:step] ), especially reversing a string with text[::-1] .
Prefer f-strings for formatting because they are the most readable and commonly
used in modern Python.
Know the difference between find() (returns -1 if not found) and index() (raises
an error if not found).
Practice common string methods such as upper() , lower() , replace() , split() ,
join() , startswith() , endswith() , and count() , as they are frequently used in
coding interviews and exams.