BCA / B.Tech 8 min read

What is String in Python? String in Hindi (Examples के साथ)

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: Python Notes in Hindi | पायथन नोट्स हिंदी में (BCA / B.Tech Syllabus)
  • • मुख्य विषय: What is String in Python? String in Hindi (Examples के साथ) - General Topics
  • • अवलोकन: Quick Answer: Python में string characters (अक्षर, अंक, symbol, space) का एक क्रम (sequence) है, जिसे single quotes '...', double quotes "..." या triple...
  • • मुख्य अवधारणाएं: Python string, Creation slicing methods formatting Hindi, Immutability string,Python string, Creation slicing methods formatting, Immutability string
Quick Answer: Python में string characters (अक्षर, अंक, symbol, space) का एक क्रम (sequence) है, जिसे single quotes '...', double quotes "..." या triple quotes '''...''' के बीच लिखा जाता है, जैसे name = "NotesMedia"। Python string का data type str है, यह immutable है (बनने के बाद बदली नहीं जा सकती), और इसके हर character को index (0, 1, 2…) से access किया जा सकता है।

String in Hindi: String क्या है?

आसान भाषा में, string = text। आपका नाम, mobile number को text की तरह लिखना, एक पूरा paragraph, ये सब strings हैं। हर programming language में text रखने के लिए string होती है।

  • "Ravi" एक string है।
  • "123" भी string है, number नहीं, क्योंकि यह quotes में है।
  • "" खाली string (empty string) है, इसकी length 0 है।

Python के बाकी data types (int, float, list, dict…) का overview यहाँ पढ़ें: Data Types in Python in Hindi।

Advertisement

What is String in Python with Example

Python में string बनाने के तीन तरीके हैं। तीनों से एक ही str type बनता है।

# Single quote string
string1 = 'यह एक string है'

# Double quote string
string2 = "यह भी एक string है"

# Triple quote string (कई lines वाली)
string3 = '''यह कई lines
की string है'''

print(type(string1))

Output: <class 'str'>

Single और double quotes में कोई फर्क नहीं है। Double quotes तब काम आते हैं जब text के अंदर apostrophe हो: "It's easy"।

"123" और 123 में अंतर

x = "123"
print(type(x))      # <class 'str'>
print(int(x) + 1)   # 124

input() हमेशा string देता है, इसलिए number चाहिए तो int() या float() से बदलना पड़ता है। Input-output detail में: Input Output Syntax in Hindi।

Python में String के प्रकार

प्रकारकैसे लिखते हैंकब use करें
Single-line string'Hello' या "Hello"Normal छोटा text
Multi-line string'''...''' या """..."""कई lines का text, docstring
Raw stringr"C:\new\test"Windows path, regular expression, जहाँ \ को वैसा ही रखना हो
f-string (formatted)f"Hi {name}"Text के बीच variable की value डालनी हो (Python 3.6+)
Unicode stringu"text"Python 3 में हर string पहले से Unicode है, u लिखने से कोई फर्क नहीं पड़ता

Raw String example

print("C:\new\test")    # \n = new line, \t = tab
print(r"C:\new\test")   # raw string, जैसा लिखा वैसा

Output:

C:
ew	est
C:\new\test

Normal string में \n नई line और \t tab बन गए। Raw string में backslash को escape character नहीं माना जाता।

f-string example

name = "Ravi"
marks = 87.456
print(f"{name} ke marks {marks:.1f} hain")

Output: Ravi ke marks 87.5 hain

Escape Sequences

String के अंदर कुछ special characters backslash (\) से लिखे जाते हैं:

  • \n: नई line। print('Line1\nLine2') दो lines में print करता है।
  • \t: tab space। print('Hello\tWorld')
  • \' और \": quote को string के अंदर लिखने के लिए, जैसे 'It\'s' का output It's।
  • \\: एक backslash print करने के लिए।

String Indexing और Slicing

String के हर character का एक index होता है। आगे से गिनती 0 से और पीछे से -1 से शुरू होती है।

  P    y    t    h    o    n
  0    1    2    3    4    5     (positive index)
 -6   -5   -4   -3   -2   -1     (negative index)
s = "Python"
print(s[0], s[5], s[-1], s[-2])   # P n n o
print(s[0:4])    # Pyth   (index 0 से 3 तक, 4 शामिल नहीं)
print(s[2:])     # thon
print(s[:3])     # Pyt
print(s[::2])    # Pto    (हर दूसरा character)
print(s[::-1])   # nohtyP (string उल्टी)
print(len(s))    # 6

Slicing का rule याद रखें: s[start:stop:step] में stop वाला index शामिल नहीं होता।

Is String Mutable in Python? (Immutable का मतलब)

नहीं। Python में string immutable है। बनने के बाद उसका कोई character बदला नहीं जा सकता।

s = "Python"
s[0] = "J"

Output: TypeError: 'str' object does not support item assignment

बदलाव चाहिए तो पुरानी string से नई string बनाइए:

s = "Python"
s = "J" + s[1:]
print(s)    # Jython

यहाँ पुरानी string नहीं बदली। s अब एक नई string की तरफ point करता है। upper(), replace() जैसे methods भी हमेशा नई string return करते हैं। इसके उलट list mutable है, उसका element बदला जा सकता है: List और Tuples in Hindi।

String Operations in Python

OperationCodeOutput
Concatenation (जोड़ना)'Hello' + ' World'Hello World
Repetition (दोहराना)'Hi' * 3HiHiHi
Membership (है या नहीं)'Py' in 'Python'True
Lengthlen('Hello')5
Slicing'Hello, World'[0:5]Hello

ध्यान दें: "Age: " + 20 से TypeError आता है। String में number जोड़ना हो तो "Age: " + str(20) या f-string लिखें।

Important String Methods (Built-in)

MethodकामExampleOutput
upper()सब capital letters'Hello, World'.upper()HELLO, WORLD
lower()सब small letters'Hello, World'.lower()hello, world
title()हर word का पहला letter capital'python is easy'.title()Python Is Easy
strip()आगे-पीछे के spaces हटाना' notes media '.strip()notes media
replace(a, b)a की जगह b'Hello, World'.replace('World', 'Python')Hello, Python
split(sep)तोड़कर list बनाना'Hello, World, Python'.split(',')['Hello', ' World', ' Python']
join(list)list को जोड़कर string' '.join(['Hello', 'World', 'Python'])Hello World Python
find(x)पहला index, न मिले तो -1'banana'.find('n')2
count(x)कितनी बार आया'banana'.count('a')3
isdigit()सिर्फ digits हैं?'2026'.isdigit()True
endswith(x)x पर खत्म होती है?'data.csv'.endswith('.csv')True

बाकी built-in methods की list: Built-in Method in Hindi।

Advertisement

String पर Loop और Comparison

हर character पर loop

String एक sequence है, इसलिए for loop से उसके हर character तक पहुँच सकते हैं। नीचे वाला program एक word में vowels गिनता है:

count = 0
for ch in "education":
    if ch in "aeiou":
        count = count + 1
print("Vowels =", count)

Output: Vowels = 5 (e, u, a, i, o)

Strings को compare करना

==, <, > से strings compare होती हैं। Comparison character-by-character उनके Unicode number से होता है, और यह case-sensitive है।

print("apple" < "banana")   # True
print("Apple" == "apple")   # False (A और a अलग हैं)
print("Zebra" < "apple")    # True, क्योंकि ord('Z')=90 और ord('a')=97

Case की परवाह किए बिना compare करना हो तो दोनों को पहले lower() कर लें।

Solved Example: Email साफ़ करना

User ने email गलत format में भरा है। उसे clean करके username और domain अलग करते हैं।

email = "  Ravi.Kumar@Gmail.COM "
e = email.strip().lower()
user, domain = e.split("@")
print(e)
print(user)
print(domain)
print(e.count("."))

Output:

ravi.kumar@gmail.com
ravi.kumar
gmail.com
2

strip() ने spaces हटाए, lower() ने सब small किया, और split("@") ने दो हिस्सों की list दी जिसे दो variables में ले लिया।

Python String vs C String

PointPythonC
TypeBuilt-in str typechar का array
आखिर मेंकुछ extra नहीं'\0' (null character)
बदल सकते हैं?नहीं (immutable)char array के characters बदल सकते हैं
Lengthlen(s)strlen(s) (string.h)
Unicode (Hindi text)सीधे supportअलग handling चाहिए

Python में Hindi text भी सीधे string में रख सकते हैं। एक बात ध्यान रखें: len("नमस्ते") का answer 6 आता है, क्योंकि len() Unicode characters गिनता है और मात्राएँ भी अलग character हैं।

Students की Common Mistakes

  • s.upper() लिखकर मान लेना कि s बदल गई। String immutable है, इसलिए s = s.upper() लिखना पड़ता है।
  • Slicing में stop index को शामिल मान लेना। "Python"[0:2] का output Py है, Pyt नहीं।
  • input() से आई value को सीधे number की तरह जोड़ना। "5" + "3" का output 53 है, 8 नहीं।
  • Index को length के बराबर लेना। "Python"[6] से IndexError आता है, क्योंकि आखिरी index 5 है।

Python String की खास बातें (एक नज़र में)

  • Immutable: बनने के बाद बदल नहीं सकती।
  • Ordered sequence: characters का order तय है, इसलिए indexing और slicing चलते हैं।
  • Unicode support: Hindi, English, emoji, सब एक ही string में।
  • Built-in methods: upper(), lower(), find(), replace(), split(), join() आदि।
  • Iterable: loop से एक-एक character पढ़ सकते हैं।

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. Python में string क्या है? Example दीजिए।
  2. String immutable है, इसका क्या मतलब है? (बनने के बाद characters बदले नहीं जा सकते)
  3. s = "Python" हो तो s[-1] और s[1:4] का output लिखिए। (n, yth)
  4. Raw string क्या है? (r prefix, backslash escape नहीं होता)
  5. split() और join() में अंतर लिखिए।

Long Questions (5–10 marks)

  1. Python में string बनाने के तरीके और string के प्रकार example के साथ समझाइए।
  2. String indexing और slicing को positive और negative index के साथ समझाइए।
  3. किन्हीं पाँच string methods को example और output के साथ लिखिए।
  4. String operations (concatenation, repetition, membership) समझाइए। String immutable क्यों है?

FAQs

What are strings in Python, short में?

Quotes के बीच लिखा कोई भी text Python में string है। इसका type str है, यह immutable है और इसके characters index से मिलते हैं।

Python में character का अलग data type होता है क्या?

नहीं। C की तरह Python में char type नहीं है। एक character भी length 1 की string है, जैसे 'A'।

String को उल्टा (reverse) कैसे करें?

Slicing से: s[::-1]। जैसे "Python"[::-1] का output nohtyP है।

format() और f-string में कौन सा use करें?

दोनों से एक ही काम होता है: "Name: {} Age: {}".format("Ravi", 20) और f"Name: {name} Age: {age}"। नया code लिखते समय f-string छोटा और पढ़ने में आसान है, पर पुराने books में format() भी दिखेगा।

find() और index() में क्या फर्क है?

दोनों पहला index बताते हैं। Value न मिले तो find() -1 देता है, जबकि index() ValueError देता है।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: Python में string characters (अक्षर, अंक, symbol, space) का एक क्रम (sequence) है, जिसे single quotes '...', double quotes "..." या triple quotes '''...''' के बीच लिखा जाता है, जैसे name = "NotesMedia"।
Python Notes in Hindi | पायथन नोट्स हिंदी में के पाठ्यक्रम में What is String in Python? String in Hindi (Examples के साथ) एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको What is String in Python? String in Hindi (Examples के साथ) के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement