0% found this document useful (0 votes)
5 views10 pages

Python PPT Unit-3

This document covers complex data types in Python, specifically focusing on strings. It explains string operations such as declaration, length, indexing, slicing, concatenation, membership, comparison, case conversion, search methods, and immutability. The document provides examples for each operation to illustrate how strings can be manipulated in Python.

Uploaded by

arvindkr808080
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Python PPT Unit-3

This document covers complex data types in Python, specifically focusing on strings. It explains string operations such as declaration, length, indexing, slicing, concatenation, membership, comparison, case conversion, search methods, and immutability. The document provides examples for each operation to illustrate how strings can be manipulated in Python.

Uploaded by

arvindkr808080
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT-3

Unit Title: Complex Data Types & Functions in Python

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 Indexing & Slicing


#Example
text = "Python"
print(text[0]) # 'P'
print(text[-1]) # 'n'
Slicing:
# Example
text = "Python"
print(text[0:3]) # 'Pyt'
print(text[2:]) # 'thon'
print(text[:4]) # 'Pyth'
print(text[-3:]) # 'hon’

String Concatenation and Repetition:


# Example
str1 = "Hello"
str2 = "World"
print(str1 + " " + str2) # 'Hello World'
print(str1 * 3) # 'HelloHelloHello'
String Membership:
# Example
print('Py' in 'Python') # True
print('Java' not in 'Python') # True

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

Strip and Replace:


# Example
data = " Hello "
print(data.strip()) # 'Hello'
print(data.lstrip()) # 'Hello '
print(data.rstrip()) # ' Hello'
print(data.replace("l", "*")) # ' He**o '
Startswith & Endswith:
#Example
filename = "data.csv"
print(filename.startswith("data")) # True
print(filename.endswith(".csv")) # True

Splitting and Joining:


sentence = "Python is fun"
words = sentence.split() # ['Python', 'is', 'fun']
print(words)

joined = "-".join(words) # 'Python-is-fun'


print(joined)
Escape Characters:
# Example
print("Hello\nWorld") # newline
print("She said \"Hi\"") # double quote
print('It\'s Python') # single quote

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

You might also like