In Python, strings are one of the most commonly used data types. A string is a sequence of characters used to represent text data such as words, sentences, or symbols.
Examples:
- "Hello"
- "Python Programming"
- "12345"
🔹 What is a String in Python?
A string is a collection of characters enclosed in:
-
Single quotes
' ' -
Double quotes
" " -
Triple quotes
''' '''or""" """
🔹 Creating Strings in Python
a = 'Hello'
b = "Python"
c = """Welcome to Python Strings"""
print(a)
print(b)
print(c)
🔸 Output:
Hello
Python
Welcome to Python Strings
🔹 Why Use Strings?
✔ Store text data
✔ Handle user input
✔ Work with files
✔ Build websites and apps
✔ Process data in AI and automation
🔹 String Indexing
Each character in a string has an index (starting from 0).
P Y T H O N
0 1 2 3 4 5
🔹 Example of Indexing
text = "Python"
print(text[0])
print(text[3])
🔸 Output:
P
h
🔹 Negative Indexing
Python also supports negative indexing:
P Y T H O N
-6 -5 -4 -3 -2 -1
text = "Python"
print(text[-1])
print(text[-2])
🔸 Output:
n
o
🔹 String Slicing
Slicing allows you to get part of a string.
🔹 Syntax:
string[start:end]
🔹 Example of Slicing
text = "Python Programming"
print(text[0:6])
🔸 Output:
Python
🔹 Skip Step in Slicing
text = "Python"
print(text[0:6:2])
🔸 Output:
Pto
🔹 String Length
text = "Hello World"
print(len(text))
🔸 Output:
11
🔹 String Concatenation
Joining two strings:
a = "Hello"
b = "Python"
print(a + " " + b)
🔸 Output:
Hello Python
🔹 String Repetition
print("Hi " * 3)
🔸 Output:
Hi Hi Hi
🔹 String Methods in Python
Python provides many built-in string methods.
🔹 1. upper()
text = "python"
print(text.upper())
🔸 Output:
PYTHON
🔹 2. lower()
text = "PYTHON"
print(text.lower())
🔸 Output:
python
🔹 3. strip()
Removes spaces:
text = " Python "
print(text.strip())
🔹 4. replace()
text = "I love Java"
print(text.replace("Java", "Python"))
🔸 Output:
I love Python
🔹 5. split()
text = "apple,banana,mango"
print(text.split(","))
🔸 Output:
['apple', 'banana', 'mango']
🔹 6. join()
words = ["Hello", "Python"]
print(" ".join(words))
🔸 Output:
Hello Python
🔹 Checking Strings
text = "Python123"
print(text.isalpha())
print(text.isdigit())
print(text.isalnum())
🔹 String Formatting
🔹 f-strings (Modern way)
name = "Alex"
age = 25
print(f"My name is {name} and I am {age} years old")
🔸 Output:
My name is Alex and I am 25 years old
🔹 Real-Life Example: Login System
username = input("Enter username: ")
if len(username) >= 5:
print("Valid username")
else:
print("Too short username")
🔹 Real-Life Example: Text Cleaner
text = " Hello Python "
clean_text = text.strip().lower()
print(clean_text)
🔹 Important String Properties
✔ Strings are immutable (cannot be changed)
✔ Strings are ordered
✔ Strings support indexing and slicing
✔ Strings are widely used in real applications
🔹 Common Mistakes
❌ Trying to modify string directly:
text = "Python"
text[0] = "J"
✔ Correct way:
text = "J" + text[1:]
🚀 Conclusion
Python strings are a fundamental part of programming used to handle text data. From simple messages to complex data processing, strings are everywhere in Python applications.
Once you master strings, you can:
- Build user interfaces
- Process text data
- Develop web applications
- Work with AI and data science


0 Comments