๐Ÿ“˜ Lesson 8: Strings and String Methods in Python

๐Ÿ”น 1. What is a String?

A string is a sequence of characters enclosed in quotes.

Python allows:

  • "double quotes"
  • 'single quotes'
  • '''triple quotes''' (multi-line)

Example 1: Creating Strings

name = "Sameer"
city = 'Hyderabad'
message = """Welcome to Python"""print(name)
print(city)
print(message)

Output

Sameer
Hyderabad
Welcome to Python

๐Ÿ“Œ Explanation
Strings store text information such as names, addresses, messages, etc.


๐Ÿ”น 2. Accessing Characters (Indexing)

Each character in a string has a position (index).

CharacterSameer
Index012345

Example 2: Indexing

name = "Sameer"print(name[0])
print(name[3])
print(name[5])

Output

S
e
r

๐Ÿ“Œ Explanation

  • Index starts from 0
  • name[0] means first character

๐Ÿ”น 3. Negative Indexing

Python also supports negative indexing.

CharacterSameer
Index-6-5-4-3-2-1

Example 3

name = "Sameer"print(name[-1])
print(name[-3])

Output

r
e

๐Ÿ”น 4. String Slicing

Slicing extracts part of a string.

Syntax

string[start : end]

Example 4

name = "Chandini"print(name[0:4])
print(name[2:6])

Output

Chan
andi

๐Ÿ“Œ Explanation

  • Start index included
  • End index excluded

๐Ÿ”น 5. String Length

len() returns number of characters.


Example 5

name = "Ayesha"print(len(name))

Output

6

๐Ÿ”น 6. Convert Case Methods


Example 6

text = "python programming"print(text.upper())
print(text.lower())
print(text.title())
print(text.capitalize())

Output

PYTHON PROGRAMMING
python programming
Python Programming
Python programming

๐Ÿ“Œ Explanation

MethodPurpose
upper()Convert to uppercase
lower()Convert to lowercase
title()Capitalize every word
capitalize()Capitalize first letter

๐Ÿ”น 7. Removing Spaces


Example 7

text = "   Sameer   "print(text.strip())

Output

Sameer

๐Ÿ“Œ Explanation
strip() removes spaces from beginning and end.


๐Ÿ”น 8. Replacing Text


Example 8

text = "Hello Sameer"print(text.replace("Sameer", "Ayesha"))

Output

Hello Ayesha

๐Ÿ”น 9. Checking Text


Example 9

text = "Python Programming"print("Python" in text)
print("Java" in text)

Output

True
False

๐Ÿ“Œ Explanation
Checks whether a word exists in the string.


๐Ÿ”น 10. Splitting Strings


Example 10

text = "Sameer,Ayesha,Saddam"print(text.split(","))

Output

['Sameer', 'Ayesha', 'Saddam']

๐Ÿ“Œ Explanation
split() converts string into list


๐Ÿ”น 11. Joining Strings


Example 11

names = ["Sameer", "Ayesha", "Gousya"]result = " - ".join(names)print(result)

Output

Sameer - Ayesha - Gousya

๐Ÿ”น 12. Checking String Type


Example 12

text = "Python123"print(text.isalpha())
print(text.isdigit())
print(text.isalnum())

Output

False
False
True

๐Ÿ“Œ Explanation

MethodMeaning
isalpha()letters only
isdigit()numbers only
isalnum()letters + numbers

๐Ÿ”น 13. String Concatenation


Example 13

first = "Sameer"
last = "Khan"print(first + " " + last)

Output

Sameer Khan

๐Ÿ”น 14. String Formatting


Example 14

name = "Chandini"
age = 22print(f"{name} is {age} years old")

Output

Chandini is 22 years old

๐Ÿ”น 15. Real-Life Example


Example 15: Username Check

username = input("Enter username: ")if username.lower() == "sameer":
print("Access granted")
else:
print("Access denied")

Sample Output

Enter username: Sameer
Access granted

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *