UNIT-2
Part2
STRING
# What is a String?
A string is a sequence of characters enclosed in quotes.
Example:
name = "Kirti"
Here "Kirti" is a string.
You can use:
Single quotes → 'Kirti'
Double quotes → " Kirti "
Triple quotes → ''' Kirti ''' or """ Kirti """ (for multi-line strings)
String Operations
1. Concatenation (joining strings)
You can join two or more strings using the + operator.
a = "Kirti"
b = "Arora"
c=a+""+b
print(c)
Output: Kirti Arora
2. Repetition
You can repeat a string using the * operator.
a = "Kirti "
print(a * 3)
Output: Kirti Kirti Kirti
3. Membership (in / not in)
You can check if a substring is present in another string.
a = "Python programming"
print("Python" in a) # True
print("Java" not in a) # True
4. Slicing
You can take a part (slice) of a string using [start:end].
a = "Python"
print(a[0:4]) # from index 0 to 3 → 'Pyth'
print(a[:3]) # same as a[0:3]
print(a[2:]) # from index 2 to end → 'thon'
print(a[-1]) # last character → 'n'
5. Traversing a String (using loops)
You can go through each character using a loop.
a = "Hello"
for ch in a:
print(ch)
Output:
H
e
l
l
o
Built-in String Functions / Methods
Here are important string methods with simple examples
1. len()
Finds the length of a string.
a = "Python"
print(len(a)) # 6
2. capitalize()
Makes the first letter uppercase and others lowercase.
a = "python is fun"
print([Link]()) # Python is fun
3. title()
Makes the first letter of every word uppercase.
a = "python is fun"
print([Link]()) # Python Is Fun
4. lower()
Converts the string to lowercase.
a = "HELLO"
print([Link]()) # hello
5. upper()
Converts the string to uppercase.
a = "hello"
print([Link]()) # HELLO
6. count()
Counts how many times a substring appears.
a = "banana"
print([Link]('a')) # 3
7. find()
Finds the first position (index) of a substring. Returns -1 if not found.
a = "hello world"
print([Link]("world")) # 6
8. index()
Similar to find(), but gives an error if the substring is not found.
a = "hello world"
print([Link]("world")) # 6
9. endswith()
Checks if a string ends with a certain substring.
a = "[Link]"
print([Link](".txt")) # True
[Link]()
Checks if a string starts with a certain substring.
a = "hello world"
print([Link]("hello")) # True
11. isalnum()
Returns True if all characters are letters or numbers (no spaces).
a = "Python123"
print([Link]()) # True
12. isalpha()
Returns True if all characters are alphabets.
a = "Python"
print([Link]()) # True
13. isdigit()
Returns True if all characters are digits.
a = "12345"
print([Link]()) # True
14. islower()
Checks if all letters are lowercase.
a = "hello"
print([Link]()) # True
15. isupper()
Checks if all letters are uppercase.
a = "HELLO"
print([Link]()) # True
16. isspace()
Checks if the string has only spaces.
a=" "
print([Link]()) # True
17. lstrip(), rstrip(), strip()
Removes spaces (or specific characters) from:
left (lstrip)
right (rstrip)
both sides (strip)
a = " Hello "
print([Link]()) # 'Hello '
print([Link]()) # ' Hello'
print([Link]()) # 'Hello'
18. replace()
Replaces one substring with another.
a = "I like Java"
print([Link]("Java", "Python")) # I like Python
19. join()
Joins elements of a list into a single string.
words = ["Python", "is", "fun"]
print(" ".join(words)) # Python is fun
20. partition()
Splits the string into three parts:
1. Part before the separator
2. The separator itself
3. Part after the separator
a = "hello world"
print([Link](" "))
# ('hello', ' ', 'world')
21. split()
Splits the string into a list of words.
a = "Python is fun"
print([Link]()) # ['Python', 'is', 'fun']
You can also specify a separator:
a = "apple,banana,grape"
print([Link](",")) # ['apple', 'banana', 'grape']
What it
Function Example Output
does
Counts
len() len("Hello") 5
letters
First letter
capitalize() "hello".capitalize() Hello
big
Each word’s Python Is
title() "python is fun".title()
first letter big Fun
All small
lower() "HELLO".lower() hello
letters
upper() All big "hi".upper() HI
What it
Function Example Output
does
letters
Counts how
many times
count() "banana".count('a') 3
something
appears
Finds where
find() something "hello world".find("world") 6
starts
Same as find
but gives
index() "hello".index("lo") 3
error if not
found
Checks if it
startswith() starts with "hello".startswith("he") True
something
Checks if it
endswith() ends with "[Link]".endswith(".txt") True
something
Letters +
isalnum() numbers "abc123".isalnum() True
only
isalpha() Only letters "abc".isalpha() True
isdigit() Only digits "123".isdigit() True
All small
islower() "hello".islower() True
letters
All big
isupper() "HELLO".isupper() True
letters
isspace() Only spaces " ".isspace() True
What it
Function Example Output
does
Removes
strip() spaces (both " hi ".strip() "hi"
sides)
Removes
lstrip() " hi".lstrip() "hi"
spaces on left
Removes
rstrip() spaces on "hi ".rstrip() "hi"
right
Changes "I like Java".replace("Java", "I like
replace()
words "Python") Python"
Joins list into " "Python
join()
string ".join(["Python","is","fun"]) is fun"
Breaks string
split() "a,b,c".split(",") ['a','b','c']
into list
Breaks into 3 ('hi','
partition() "hi world".partition(" ")
parts ','world')