इस सेक्शन में हम python string in hindi के 20 ज़रूरी methods को छोटे-छोटे examples के साथ समझेंगे। Copy-बटन से सीधे कोड उठाएँ और चलाएँ।
s = "hello world"
print(s.upper()) # HELLO WORLD
print(s.lower()) # hello world
print(s.title()) # Hello World
print(s.capitalize()) # Hello world
s = " **hello** "
print(s.strip()) # '**hello**'
print(s.strip(" *")) # 'hello'
print(s.lstrip()) # '**hello** '
print(s.rstrip()) # ' **hello**'
text = "2024-12-31"
print(text.replace("-", "/")) # 2024/12/31
print("aaaa".replace("a","A",2)) # AAaa (count=2)
"a,b,c".split(",") # ['a','b','c']
"a,b,c".rsplit(",", 1) # ['a,b','c']
"line1\nline2".splitlines()# ['line1','line2']
items = ["A","B","C"]
print(" | ".join(items)) # A | B | C
s = "abracadabra"
print(s.find("bra")) # 1 (or -1)
print(s.rfind("bra")) # 8
print(s.index("cad")) # 4 (ValueError if not found)
s = "banana"
print(s.count("an")) # 2
print(s.count("a", 1)) # from index 1 → 2
print("hello.py".endswith(".py")) # True
print("Mr. John".startswith("Mr")) # True
print("42".zfill(5)) # 00042
print("hi".rjust(5,"-")) # ---hi
print("hi".ljust(5,"-")) # hi---
print("hi".center(6,"*")) # **hi**
s = "key=value=extra"
print(s.partition("=")) # ('key', '=', 'value=extra')
print(s.rpartition("=")) # ('key=value', '=', 'extra')
print("ABC".isalpha()) # True
print("123".isdigit()) # True
print("१२३".isnumeric()) # True (Devanagari)
print("A1".isalnum()) # True
print(" ".isspace()) # True
# बेहतर lower() वैरिएंट: Unicode friendly
print("Straße".lower() == "strasse") # False
print("Straße".casefold() == "strasse") # True
data = "नमस्ते"
b = data.encode("utf-8") # bytes
print(type(b), b)
print(b.decode("utf-8")) # back to str
tbl = str.maketrans({"a":"@", "e":"3"})
print("peace".translate(tbl)) # p3@c3
name, price = "Vista", 249.9
print("Hello {}, ₹{:.2f}".format(name, price))
print(f"Hello {name}, ₹{price:.2f}") # preferred
print("PyThOn".swapcase()) # pYtHoN
print("a\tb\tc".expandtabs(4)) # tab → 4 spaces
fn = "report.xlsx"
print(fn.endswith((".xlsx",".csv"))) # True
txt = "Spam!!! Buy!!!"
clean = txt.replace("!", "")
print(clean, "removed:", txt.count("!"))
s = "0123456789"
print(s[::-1]) # reverse
print(s[::2]) # step 2 → 02468
print(s[-4:]) # last 4 → 6789
इन problems से आपकी python string in hindi समझ practically मजबूत होगी—cleaning, validation, formatting, parsing और छोटे-छोटे real-world tasks पर ध्यान।
जाँचें कि string में ‘@’ और ‘.’ मौजूद हैं (very basic).
‘@’ in s and ‘.’ in s
s = "user@example.com"
is_basic_email = ("@" in s) and ("." in s)
print(is_basic_email)
lower + trim spaces + internal spaces को single ‘-‘ में बदलें।
s.strip().lower().split()
raw = " Vista Academy "
norm = "-".join(raw.strip().lower().split())
print(norm) # vista-academy
name को clean करके Title Case में बदलें।
strip + lower + title()
name = " viSTA aCAdemy "
clean = name.strip().lower().title()
print(clean) # 'Vista Academy'
string में vowels की गिनती करें (a, e, i, o, u).
sum(ch in ‘aeiou’)
txt = "Data Science"
v = sum(ch.lower() in "aeiou" for ch in txt)
print(v)
text से !?,.:- जैसे punctuation हटाएँ।
translate + maketrans
punc = "!?.,:-;\"'"
table = str.maketrans("", "", punc)
print("Hello, World!!!".translate(table)) # Hello World
string में मौजूद digits का sum निकालें (char-wise).
ch.isdigit()
s = "a1b2c3"
total = sum(int(ch) for ch in s if ch.isdigit())
print(total) # 6
“9876543210” → “******3210”
s[-4:] + ‘*’ * (len(s)-4) (order ठीक रखें)
ph = "9876543210"
masked = "*" * (len(ph) - 4) + ph[-4:]
print(masked)
Title → kebab-case slug (letters/digits/hyphen).
lower + replace spaces → ‘-‘ + regex non-alnum drop
import re
title = "Python String in Hindi — Best Guide!"
s = title.lower().strip().replace(" ", "-")
s = re.sub(r"[^a-z0-9\-]+", "", s)
s = re.sub(r"-+", "-", s).strip("-")
print(s)
spaces ignore करें, lowercase में count करके top char बताएं।
max(set(s), key=s.count)
txt = "Better Data Better World"
clean = txt.replace(" ", "").lower()
top = max(set(clean), key=clean.count)
print(top)
non-alphanumeric हटाकर palindrome जाँचें।
filter(str.isalnum, s.lower())[::-1]
s = "A man, a plan, a canal: Panama!"
clean = "".join(ch for ch in s.lower() if ch.isalnum())
print(clean == clean[::-1])
String characters का ordered sequence है, जैसे "Hello" या 'नमस्ते'। यह immutable होती है—बदलने पर नया object बनता है।
Single ('..') और Double ("..") एक जैसे हैं; Triple quotes (''' ... ''' या """ ... """) multi-line strings के लिए उपयोग करें।
Indexing: s[i] (0-based). Slicing: s[start:stop:step].
उदाहरण: "python"[1:4] → 'yth', "abc"[::-1] → 'cba'.
upper(), lower(), strip(), replace(), split(), join(), find(), startswith(), endswith(), count() आदि—ऊपर दिए गए demos देखें।
f-strings readable और fast होती हैं: f"Total: ₹{price:.2f}". यह .format() या % से concise और modern है।
Python 3 में strings Unicode हैं—UTF-8 encode/decode करें: s.encode("utf-8") और b.decode("utf-8").
Case-insensitive compare के लिए casefold() उपयोगी है।
अब जब आपने Python String in Hindi सीख लिया है, तो अपने Skills को अगले लेवल पर ले जाएं इन Guides और Project के साथ।