Python String क्या है in Hindi

Arpit Nageshwar
⏰ 5 min read

Python Strings in Hindi – String Kya Hai? Introduction, Characteristics, Indexing, Slicing aur Operations | Complete Guide

Table of Contents

Introduction to Strings in Hindi – String Kya Hai?

  • Python में String एक ऐसा data type होता है जिसमें characters की एक sequence store की जाती है, यानी अगर आप कोई भी text, नाम, sentence या message store करना चाहते हैं, तो उसके लिए String का ही इस्तेमाल होता है।

  • String को हमेशा single quotes (' '), double quotes (" ") या triple quotes (''' ''' या """ """) के अंदर लिखा जाता है, जैसे 'Hello', "Python" या '''यह एक line है'''।

  • अगर आप सोचें तो String को एक ऐसी माला की तरह समझ सकते हैं जिसमें हर character एक मोती (bead) की तरह fixed position पर टंगा हुआ होता है, और पूरी माला मिलाकर एक complete word या sentence बनाती है।

  • Python में String एक built-in data type है, इसका मतलब है कि इसे इस्तेमाल करने के लिए आपको कोई अलग से library import करने की जरूरत नहीं पड़ती, यह directly Python में ready-to-use मिल जाता है।

  • Exam के point of view से यह याद रखना जरूरी है कि String class का नाम str होता है, और type() function से आप किसी भी variable का data type check कर सकते हैं।

name = "Python" यहाँ name एक variable है और "Python" एक String value है type(name) → <class 'str'>

यह diagram दिखाता है कि Python में एक string variable कैसे declare होता है और उसका data type str कैसे check किया जाता है।

Characteristics of Strings in Hindi – String की विशेषताएं

  • 1. Sequence of Characters:
    String characters की एक ordered sequence होती है, यानी हर character का एक fixed position (index) होता है और यह order कभी अपने आप change नहीं होता।

  • 2. Immutable होती है:
    एक बार string बन जाने के बाद उसके individual characters को directly change नहीं किया जा सकता, इसे बदलने के लिए हमेशा एक नई string बनानी पड़ती है।

  • 3. Indexing Support करती है:
    String के हर character को एक positive या negative index number से access किया जा सकता है, जिससे किसी भी particular character तक सीधे पहुंचा जा सकता है।

  • 4. Iterable होती है:
    String को loop (जैसे for loop) की मदद से एक-एक character करके traverse किया जा सकता है, क्योंकि यह एक iterable object है।

  • 5. Multiple Data Types को Represent कर सकती है:
    String में सिर्फ letters ही नहीं, बल्कि numbers, symbols, spaces और special characters भी store किए जा सकते हैं, क्योंकि Python में यह सब text ही माना जाता है।

  • 6. Concatenation और Repetition Support करती है:
    दो या ज्यादा strings को + operator से जोड़ा जा सकता है, और * operator से किसी string को कई बार repeat भी किया जा सकता है।

Immutable Nature of Strings in Hindi – String Immutable क्यों होता है?

  • Immutable का मतलब क्या है:
    Immutable का मतलब होता है "अपरिवर्तनीय", यानी एक बार string object create हो जाने के बाद उसकी memory में मौजूद value को directly बदला नहीं जा सकता।

    अगर आप किसी existing string के किसी character को change करने की कोशिश करेंगे, तो Python एक Error देगा।

  • Example से समझें:

    word = "Hello"
    word[0] = "J"   # यह line Error देगी
    # TypeError: 'str' object does not support item assignment

    ऊपर दिए गए code में हम पहले character "H" को "J" से replace करना चाह रहे हैं, लेकिन चूंकि string immutable है, इसलिए Python इसे allow नहीं करता।

  • तो बदलाव कैसे करें:
    अगर string में कोई change चाहिए, तो एक नई string बनानी पड़ती है, पुरानी string अपनी जगह same रहती है।

    word = "Hello"
    new_word = "J" + word[1:]
    print(new_word)   # Output: Jello
  • Immutable होने का फायदा:
    चूंकि string का value fix रहता है, इसलिए इसे hashable बनाया जा सकता है, जिससे इसे dictionary keys या set elements की तरह safely use किया जा सकता है। इसके अलावा memory management भी ज्यादा efficient हो जाती है, क्योंकि same value वाली strings को Python internally एक ही जगह reuse कर सकता है।

word = "Hello" change try TypeError (Not Allowed) नया बनाओ new_word = "Jello"

यह diagram दिखाता है कि original string "Hello" को directly change नहीं किया जा सकता, इसलिए change के लिए एक बिल्कुल नई string बनानी पड़ती है।

String Indexing in Hindi – String Indexing कैसे काम करती है?

  • Indexing क्या होती है:
    Indexing की मदद से string के किसी भी particular character को उसकी position (index number) के आधार पर access किया जाता है।

  • Positive Indexing:
    Python में indexing हमेशा 0 से शुरू होती है, यानी string के पहले character का index 0 होता है, दूसरे का 1, तीसरे का 2, और इसी तरह आगे बढ़ता जाता है।

  • Negative Indexing:
    Python एक extra feature भी देता है जिसमें आप string को पीछे से भी access कर सकते हैं, इसमें last character का index -1 होता है, उससे पहले वाले का -2, और इसी तरह आगे।

  • Example से समझें:

    word = "PYTHON"
    print(word[0])    # Output: P (पहला character)
    print(word[3])    # Output: H (चौथा character)
    print(word[-1])   # Output: N (last character)
    print(word[-3])   # Output: H (पीछे से तीसरा character)
  • Error से बचना:
    अगर आप string की length से बड़ा कोई index number use करेंगे, तो Python IndexError: string index out of range देगा, इसलिए हमेशा valid range के अंदर ही index use करना चाहिए।

String Indexing – "PYTHON" P Y T H O N 0 1 2 3 4 5 -6 -5 -4 -3 -2 -1 ऊपर वाली row Positive Index है, नीचे वाली row Negative Index है

यह diagram दिखाता है कि "PYTHON" शब्द के हर character को positive index (0 से शुरू) और negative index (-1 से पीछे की तरफ) दोनों तरीकों से कैसे access किया जा सकता है।

String Slicing in Hindi – String Slicing कैसे करें?

  • Slicing क्या होती है:
    Slicing की मदद से string में से characters का एक पूरा हिस्सा (substring) एक साथ निकाला जाता है, यह single character access करने से अलग है क्योंकि इसमें एक range दी जाती है।

  • Syntax:

    string[start : stop : step]

    इसमें start वह index है जहाँ से slicing शुरू होगी, stop वह index है जहाँ तक slicing होगी (यह index खुद include नहीं होता), और step यह बताता है कि कितने-कितने characters छोड़कर उठाना है।

  • Example से समझें:

    word = "PYTHON"
    print(word[1:4])    # Output: YTH
    print(word[:3])     # Output: PYT (start छोड़ने पर शुरुआत से)
    print(word[3:])     # Output: HON (stop छोड़ने पर आखिर तक)
    print(word[::-1])   # Output: NOHTYP (पूरी string reverse)
    print(word[::2])    # Output: PTO (हर दूसरा character)
  • Exam के लिए Important Point:
    Slicing करते समय stop index वाला character कभी भी result में शामिल नहीं होता, यह सिर्फ एक boundary का काम करता है, इस बात को अक्सर students भूल जाते हैं और गलत output निकाल लेते हैं।

Slicing Example – word[1:4] P Y T H O N 0 1 2 3 4 5 YTH Dark boxes select होते हैं, index 4 (O) slice में include नहीं होता

यह diagram दिखाता है कि word[1:4] लिखने पर index 1 से लेकर index 3 तक के characters select होते हैं और result "YTH" मिलता है, जबकि index 4 वाला character शामिल नहीं होता।

String Concatenation in Hindi – Strings को Join कैसे करें?

  • Concatenation क्या होता है:
    दो या दो से ज्यादा strings को आपस में जोड़कर एक नई single string बनाने की process को Concatenation कहा जाता है।

  • + Operator से Concatenation:

    first_name = "Rahul"
    last_name = "Sharma"
    full_name = first_name + " " + last_name
    print(full_name)   # Output: Rahul Sharma

    यहाँ ध्यान देने वाली बात यह है कि दोनों names के बीच space देने के लिए हमें अलग से " " string add करनी पड़ी, वरना दोनों नाम बिना space के जुड़ जाते।

  • * Operator से Repetition:

    print("ab" * 3)   # Output: ababab

    यह + से थोड़ा अलग है, इसमें एक ही string को कई बार repeat किया जाता है, न कि दो अलग strings को जोड़ा जाता है।

  • join() Method से Concatenation:
    अगर आपके पास कई सारी strings एक list में हों, तो उन्हें efficiently जोड़ने के लिए join() method use किया जाता है।

    words = ["Python", "is", "fun"]
    sentence = " ".join(words)
    print(sentence)   # Output: Python is fun
  • Important Point (Type Error से बचना):
    String को directly किसी number के साथ + operator से जोड़ने पर Python Error देता है, ऐसे में number को पहले str() से string में convert करना पड़ता है।

    age = 20
    # print("Age: " + age)     # यह Error देगा
    print("Age: " + str(age))  # सही तरीका, Output: Age: 20

Common String Operations in Hindi – String के Common Operations

Common String Operations String Operations len() String की length निकालना upper() / lower() Case बदलना split() String को tokens में तोड़ना join() Tokens को जोड़ना replace() Part को बदलना find() Position ढूंढना strip() Extra spaces हटाना

यह diagram Python में इस्तेमाल होने वाले सबसे common string methods को दिखाता है, जो लगभग हर program में बार-बार use होते हैं।

  • 1. len() – Length निकालना:

    word = "Python"
    print(len(word))   # Output: 6
  • 2. upper() और lower() – Case बदलना:

    text = "Hello World"
    print(text.upper())   # Output: HELLO WORLD
    print(text.lower())   # Output: hello world
  • 3. split() – String को तोड़ना:

    sentence = "Python is easy"
    words = sentence.split()
    print(words)   # Output: ['Python', 'is', 'easy']

    यहाँ ध्यान रखें कि split() by default space से string को तोड़ता है, लेकिन आप brackets के अंदर कोई भी दूसरा character (जैसे comma) भी pass कर सकते हैं।

  • 4. replace() – Part को बदलना:

    text = "I like Java"
    new_text = text.replace("Java", "Python")
    print(new_text)   # Output: I like Python
  • 5. find() – Position ढूंढना:

    text = "Hello Python"
    print(text.find("Python"))   # Output: 6 (index जहाँ से word शुरू होता है)

    अगर word string में मौजूद ही न हो, तो find() -1 return करता है, जबकि index() इस स्थिति में सीधे Error throw कर देता है, यह difference exam में अक्सर पूछा जाता है।

  • 6. strip() – Extra Spaces हटाना:

    text = "   Hello Python   "
    print(text.strip())   # Output: "Hello Python"
  • 7. count() – Repetition गिनना:

    text = "banana"
    print(text.count("a"))   # Output: 3

Frequently Asked Questions (FAQ) – Python Strings in Hindi

Python में characters की sequence को String कहते हैं, जिसे single, double या triple quotes के अंदर लिखा जाता है और इसका data type str होता है।

String Immutable इसलिए होती है ताकि उसे safely dictionary keys, set elements जैसी जगहों पर use किया जा सके और Python memory को efficient तरीके से manage कर सके, इस वजह से string बनने के बाद उसके characters को directly बदला नहीं जा सकता।

Indexing की मदद से string का सिर्फ एक single character access किया जाता है, जबकि Slicing की मदद से एक range देकर string का पूरा हिस्सा (substring) एक साथ निकाला जाता है।

Strings को + operator, join() method या f-string formatting जैसे कई तरीकों से concatenate किया जा सकता है, इनमें से join() method कई सारी strings को जोड़ने के लिए सबसे efficient माना जाता है।

अगर searched substring string में मौजूद न हो, तो find() method -1 return करता है, जबकि index() method उसी स्थिति में ValueError throw कर देता है, बाकी दोनों methods का काम एक जैसा ही है।

Arpit Nageshwar

✍️ Arpit Nageshwar

Post-graduated | Web Developer | +3 yr Experience | IIT Kharagpur Certified