Python Tutorial - Strings
๐ค Strings - Text/Words Handle Karo
๐ Real Life Mein:
Text har jagah hai:
๐ฌ "WhatsApp messages" - Strings
๐ค "Naam, address, email" - Strings
๐ "Password, username" - Strings
๐ง "Email templates" - Long strings
Python mein text ko String kehte hain!
๐ String Banane Ke Tarike
Different Ways to Create Strings
# Single quotes
name = 'Rohan'
# Double quotes
city = "Mumbai"
# Triple quotes - Multi-line
address = """
123, MG Road
Mumbai, Maharashtra
India
"""
๐ String Operations
๐ท๏ธ Name Badge Generator
# User details
first_name = "Rahul"
last_name = "Sharma"
company = "Google"
# Concatenation (+)
full_name = first_name + " " + last_name
badge = "Hi, I am " + full_name
print(badge) # Hi, I am Rahul Sharma
# f-strings (Modern way)
badge_new = f"Hi, I am {full_name} from {company}"
print(badge_new)
โ๏ธ Useful String Methods
| Method | Kya Karta Hai | Example |
|---|---|---|
.upper() | CAPITAL letters | "hello".upper() โ "HELLO" |
.lower() | small letters | "HELLO".lower() โ "hello" |
.title() | Title Case | "rahul sharma".title() โ "Rahul Sharma" |
.replace() | Replace karo | "Hi".replace("i","ello") โ "Hello" |
.split() | Todke list banao | "a,b,c".split(",") โ ['a','b','c'] |
.strip() | Extra spaces hatao | " hi ".strip() โ "hi" |
๐ง Email Validator
email = " RAHUL@Gmail.COM "
# Clean up
email = email.strip() # Extra spaces hatao
email = email.lower() # Lowercase banao
# Check
if "@" in email and ".com" in email:
print("Valid email!")
else:
print("Invalid email!")
Example
name = "python"
print(name.upper())