๐น 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).
| Character | S | a | m | e | e | r |
|---|---|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
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.
| Character | S | a | m | e | e | r |
|---|---|---|---|---|---|---|
| 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
| Method | Purpose |
|---|---|
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
๐ Explanationstrip() 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']
๐ Explanationsplit() 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
| Method | Meaning |
|---|---|
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

Leave a Reply