Python me Strings kya hoti hain?
String ek text data type hai jisme hum letters, words, sentences, numbers ya koi bhi characters store kar sakte hain. Python me string ko single quotes (' '), double quotes (" ") ya triple quotes (''' ''' ya """ """) me likha jaata hai.
Is page me yeh sab sikhenge :-
- 1. String banana — different ways
- 2. Indexing aur Slicing
- 3. String Concatenation aur Repetition
- 4. f-String aur format()
- 5. Escape Characters
- 6. String Methods — upper, lower, strip, replace, split, find, aur bahut kuch
Note :- Python me String immutable hoti hai — matlab ek baar banne ke baad string ke andar ka koi character directly change nahi ho sakta. Har string method naya string return karta hai, original change nahi hota.
1. String Banana — Different Ways
Python me string banane ke 4 tarike hain. Teeno quotes me se koi bhi use kar sakte hain, lekin triple quotes multi-line strings ke liye use hote hain.
Example 1: Single, double aur triple quotes
Example 2: String me quotes ke andar quotes likhna
2. Indexing aur Slicing
String ke har character ka ek index (number) hota hai. Python me indexing 0 se shuru hoti hai. Negative index end se shuru hota hai.
| String | P | y | t | h | o | n |
|---|---|---|---|---|---|---|
| Positive Index | 0 | 1 | 2 | 3 | 4 | 5 |
| Negative Index | -6 | -5 | -4 | -3 | -2 | -1 |
Example 1: Indexing — kisi bhi ek character access karna
Example 2: Slicing — string ka ek hissa nikalna [start:stop:step]
Example 3: Slicing se Palindrome check karna
3. String Concatenation aur Repetition
Concatenation ka matlab hai do ya zyada strings ko jodna — + operator se. Repetition ka matlab hai string ko baar baar repeat karna — * operator se.
Example 1: Concatenation — strings jodhna
Example 2: Repetition — string ko repeat karna
4. f-String aur format()
String ke andar variables ki values seedha daal ne ke liye f-string ya format() use karte hain. f-string modern aur easy tarika hai.
Example 1: f-String — curly braces me variable daalo
Example 2: format() method — purana tarika
5. Escape Characters
Kuch special characters string me directly nahi likh sakte — jaise newline, tab ya quotes. Inke liye backslash (\) se shuru hone wale escape characters use hote hain.
| Escape Char | Matlab |
|---|---|
| \n | New line (naya line) |
| \t | Tab (space) |
| \\ | Backslash (\) |
| \' | Single quote |
| \" | Double quote |
Example 1: \n aur \t ka use
Example 2: Quotes aur backslash escape
6. String Methods
Python me strings ke saath bahut saare built-in methods aate hain jo string pe kaam karte hain. Ye methods original string change nahi karte — naya string return karte hain.
Example 1: Case methods — upper, lower, title, capitalize, swapcase
Example 2: strip() — extra spaces hatana
Example 3: find(), count() aur replace()
Example 4: split() aur join()
Example 5: Check karne wale methods — startswith, endswith, isdigit, isalpha
Example 6: Alignment methods — center, ljust, rjust, zfill
Quick Summary — Important String Methods ek nazar me
| Method | Kaam | Example |
|---|---|---|
| upper() | Sab capitals | "hi".upper() → "HI" |
| lower() | Sab small | "HI".lower() → "hi" |
| title() | Har word capital | "hi world".title() → "Hi World" |
| strip() | Spaces hatao | " hi ".strip() → "hi" |
| replace() | Text badlo | "hi".replace("h","b") → "bi" |
| split() | List me todo | "a b".split() → ["a","b"] |
| join() | List se jodo | "-".join(["a","b"]) → "a-b" |
| find() | Index dhundo | "hi".find("i") → 1 |
| count() | Kitni baar aaya | "aaa".count("a") → 3 |
| startswith() | Kya isse shuru hota hai | "hi".startswith("h") → True |
| endswith() | Kya isme khatam hota hai | "hi".endswith("i") → True |
| isdigit() | Sirf numbers hain? | "123".isdigit() → True |
| isalpha() | Sirf letters hain? | "abc".isalpha() → True |