Lesson 04 � Foundation
TEXT KO
TOOL BANAO.
Names, emails, prompts, URLs, file content�real applications ka bahut data text hota hai. Python string text ko store karne ke saath character-by-character access aur transform karne bhi deta hai.
WHAT: string ek ordered sequence hai
String quotes ke andar banta hai. Uska har character ek index par hota hai, jo 0 se start hota hai. Isliye "AI"[0] ka output A hai.
tool = "Python"
print(tool[0]) # P
print(tool[-1]) # n
print(tool[0:3]) # PytEk character choose karna: tool[0].
Range choose karna: tool[start:end]; end include nahi hota.
String ke existing character ko directly overwrite nahi kar sakte; nayi string banti hai.
WHEN: messy text ko clean karna
User input aur external data mein extra spaces ya inconsistent capitals normal hote hain. String methods original string ko nahi, usually new transformed string return karte hain.
raw_name = " riya gupta "
clean_name = raw_name.strip().title()
print(clean_name) # Riya Gupta
print(clean_name.upper()) # RIYA GUPTAUseful methods: lower(), upper(), strip(), replace(), split() aur startswith(). Inhe blindly chain karne se pehle input/output ka chhota example socho.
HOW: f-string ke saath readable output
Text aur variable ko join karne ka clean modern way f-string hai. String ke before f, aur variable braces ke andar.
Quick check
String data ko uppercase banane ki valid Python expression likho.
String ke baad dot laga kar uppercase method call karo.
Common mistakes
name[0] = "R"error dega; strings immutable hain.- Slicing mein end index include nahi hota:
text[0:3]first three chars deta hai. - Quotes ke beech apostrophe use karna ho to outer quotes ka type change karo ya escape use karo.
Ab multiple values ko ek structure mein store karna seekho�Python list ke saath.