Python PPT Unit-3
Python PPT Unit-3
Objective:
To understand and work with Python's complex data types like Strings, Lists, Tuples, and
Dictionaries. Learn how to manipulate them and organize reusable code using functions.
STRING DATA TYPE & STRING
OPERATIONS
What is a String?
A string is a sequence of characters and can contain letters, numbers, symbols and even spaces.
It must be enclosed in quotes (‘ ' or ” ") and String is Immutable Data Type.
(or)
A string is a sequence of characters enclosed in single, double, or triple quotes.
String Declaration:
str1 = 'Hello’
str2 = "Python Programming”
str3 = '''This is a multi-line string'''
STRING LENGTH
# Example
text = "Python"
print(len(text)) # Output: 6
String Comparison:
# Example
print("apple" == "Apple") # False
print("abc" < "abd") # True (lexicographically)
Case Conversion:
# Example
text = "Hello World"
print(text.lower()) # hello world
print(text.upper()) # HELLO WORLD
print(text.title()) # Hello World
print(text.capitalize())# Hello world
print(text.swapcase()) # hELLO wORLD
Search Methods:
# Example
text = "Hello Python"
print(text.find("Python")) #6
print(text.index("Hello")) #0
print(text.count("o")) #2
String Immutability:
# Example
text = "Python"
# text[0] = 'J' ❌ This will give error
text = "Java" # ✅ You can reassign, but not modify in place
STRING IMMUTABILITY
text = "Python"
# text[0] = 'J' ❌ This will give error
text = "Java" # ✅ You can reassign, but not modify in place