मुख्य कंटेंट पर जाएं
Python Strings (String, Indexing, Slicing, Methods और Formatting) in Hindi
PYTHON PROGRAMMING 13 मिनट पढ़ें 1 बार पढ़ा गया

Python Strings (String, Indexing, Slicing, Methods और Formatting) in Hindi

Lavkush Chakrawarti

परिचय

String क्या है?

उदाहरण

String की विशेषताएँ

String कैसे बनाएँ?

1. Single Quotes

2. Double Quotes

3. Triple Quotes

Empty String

String Length

उदाहरण

String Indexing

Positive Indexing

Negative Indexing

String Slicing

Syntax

उदाहरण 1

उदाहरण 2

उदाहरण 3

उदाहरण 4

उदाहरण 5

Step के साथ Slicing

Reverse String

String Immutable क्यों है?

String Concatenation

String Repetition

Membership Operators

String Comparison

Escape Characters

उदाहरण

Raw String

String Formatting

1. Comma Method

2. format()

3. f-string (Recommended)

Built-in String Functions

उदाहरण

String Methods (40+)

Practical Programs

Program 1 – String Length

Program 2 – Reverse String

Program 3 – Palindrome Check

Program 4 – Count Vowels

Program 5 – Count Characters

Common Errors

1. IndexError

2. TypeError

Best Practices

परीक्षा की दृष्टि से महत्वपूर्ण तथ्य

Frequently Asked Questions (FAQs)

प्रश्न 1. String क्या है?

प्रश्न 2. Python में String Mutable है या Immutable?

प्रश्न 3. String की Length कैसे ज्ञात करते हैं?

प्रश्न 4. String Reverse कैसे करते हैं?

प्रश्न 5. सबसे अच्छा String Formatting तरीका कौन-सा है?

Quick Revision

अगला अध्याय

 

परिचय

Programming में Text Data सबसे अधिक उपयोग होने वाले Data Types में से एक है। किसी व्यक्ति का नाम, पता, मोबाइल नंबर, ईमेल, शहर का नाम, संदेश (Message), पासवर्ड या किसी वेबसाइट का URL—ये सभी String के रूप में Store किए जाते हैं।

Python में String एक Built-in Data Type है, जिसका उपयोग Characters (अक्षरों), Numbers, Symbols तथा Special Characters के समूह (Sequence) को Store करने के लिए किया जाता है।

Python में String Immutable होती है, अर्थात एक बार String बनने के बाद उसके Characters को सीधे बदला नहीं जा सकता। यदि बदलाव करना हो, तो नई String बनाई जाती है।

इस अध्याय में हम String की मूल अवधारणा से लेकर String Creation, Indexing, Slicing, Operators, Built-in Functions, Formatting, Escape Characters, 40+ String Methods और Practical Programs तक सभी विषयों को विस्तार से समझेंगे।

 

String क्या है?

String Characters (अक्षरों), Numbers, Symbols या Special Characters का एक क्रम (Sequence) होता है, जिसे Single Quotes (' '), Double Quotes (" ") या Triple Quotes (''' ''' / """ """) के अंदर लिखा जाता है।

उदाहरण

name = "Rahul"

city = 'Lucknow'

message = """Welcome to Python"""

यहाँ तीनों Variables String हैं।

 

String की विशेषताएँ

  • String एक Sequence Data Type है।
  • यह Immutable होती है।
  • इसमें Letters, Numbers, Symbols और Spaces हो सकते हैं।
  • String में प्रत्येक Character का एक Index होता है।
  • String पर Indexing और Slicing की जा सकती है।

 

String कैसे बनाएँ?

  1. Single Quotes

name = 'Python'

 

  1. Double Quotes

course = "Python Programming"

 

  1. Triple Quotes

Triple Quotes का उपयोग Multi-line String या Documentation (Docstring) के लिए किया जाता है।

text = """Python is

easy to learn

and powerful."""

 

Empty String

text = ""

या

text = ''

 

String Length

किसी String में कुल Characters की संख्या जानने के लिए len() Function का उपयोग किया जाता है।

उदाहरण

name = "Python"

 

print(len(name))

Output

6

 

String Indexing

Python में प्रत्येक Character का एक Position Number (Index) होता है।

उदाहरण:

P   y   t   h   o   n

0   1   2   3   4   5

Negative Index:

P   y   t   h   o   n

-6 -5 -4 -3 -2 -1

 

Positive Indexing

text = "Python"

 

print(text[0])

print(text[3])

Output

P

h

 

Negative Indexing

text = "Python"

 

print(text[-1])

print(text[-2])

Output

n

o

 

String Slicing

Slicing का उपयोग String का कोई भाग (Part) निकालने के लिए किया जाता है।

Syntax

string[start:stop:step]

 

उदाहरण 1

text = "Python Programming"

 

print(text[0:6])

Output

Python

 

उदाहरण 2

print(text[7:18])

Output

Programming

 

उदाहरण 3

print(text[:6])

Output

Python

 

उदाहरण 4

print(text[7:])

Output

Programming

 

उदाहरण 5

print(text[-11:])

Output

Programming

 

Step के साथ Slicing

text = "PYTHON"

 

print(text[::2])

Output

PTO

 

Reverse String

text = "Python"

 

print(text[::-1])

Output

nohtyP

 

String Immutable क्यों है?

Python में String बनने के बाद उसके Characters को सीधे बदला नहीं जा सकता।

❌ गलत

name = "Python"

 

name[0] = "J"

Output

TypeError: 'str' object does not support item assignment

✔ सही तरीका

name = "Python"

 

name = "Jython"

 

String Concatenation

दो या अधिक Strings को जोड़ने के लिए + Operator का उपयोग किया जाता है।

first = "Hello"

second = "Python"

 

print(first + " " + second)

Output

Hello Python

 

String Repetition

* Operator की सहायता से String को कई बार दोहराया जा सकता है।

print("Hi " * 3)

Output

Hi Hi Hi

 

Membership Operators

text = "Python Programming"

 

print("Python" in text)

print("Java" not in text)

Output

True

True

 

String Comparison

print("ABC" == "ABC")

print("Python" != "Java")

 

Escape Characters

Escape Character

कार्य

\n

नई लाइन

\t

Tab

\\

Backslash

\"

Double Quote

\'

Single Quote

 

उदाहरण

print("Python\nProgramming")

 

print("Name\tMarks")

 

Raw String

यदि Escape Characters को सामान्य Text की तरह Print करना हो, तो r Prefix का उपयोग किया जाता है।

print(r"C:\Python\Files")

Output

C:\Python\Files

 

String Formatting

  1. Comma Method

name = "Rahul"

 

print("Welcome", name)

 

  1. format()

name = "Rahul"

 

print("Welcome {}".format(name))

 

  1. f-string (Recommended)

name = "Rahul"

marks = 95

 

print(f"Name : {name}")

print(f"Marks : {marks}")

 

Built-in String Functions

Function

उपयोग

len()

Length

max()

सबसे बड़ा Character

min()

सबसे छोटा Character

sorted()

Sorting

type()

Data Type

 

उदाहरण

text = "Python"

 

print(len(text))

print(max(text))

print(min(text))

 

String Methods (40+)

Python में String को Modify, Search, Format और Analyze करने के लिए अनेक Methods उपलब्ध हैं।

मुख्य Methods:

  • upper()
  • lower()
  • capitalize()
  • title()
  • swapcase()
  • casefold()
  • strip()
  • lstrip()
  • rstrip()
  • replace()
  • split()
  • join()
  • find()
  • index()
  • count()
  • startswith()
  • endswith()
  • isalpha()
  • isdigit()
  • isalnum()
  • isspace()
  • islower()
  • isupper()
  • istitle()
  • center()
  • ljust()
  • rjust()
  • zfill()
  • partition()
  • rpartition()
  • splitlines()
  • encode()
  • expandtabs()
  • format()
  • format_map()
  • maketrans()
  • translate()
  • removeprefix()
  • removesuffix()

नोट: इतने महत्वपूर्ण Methods होने के कारण प्रत्येक Method को अलग-अलग अध्याय में विस्तार से समझाया जाएगा, ताकि हर Method के Syntax, Examples, Practical Programs और Interview Questions को अच्छे से कवर किया जा सके।

 

Practical Programs

Program 1 – String Length

text = input("Enter Text: ")

 

print("Length =", len(text))

 

Program 2 – Reverse String

text = input("Enter String: ")

 

print(text[::-1])

 

Program 3 – Palindrome Check

text = input("Enter String: ")

 

if text == text[::-1]:

    print("Palindrome")

else:

    print("Not Palindrome")

 

Program 4 – Count Vowels

text = input("Enter String: ").lower()

 

count = 0

 

for ch in text:

    if ch in "aeiou":

        count += 1

 

print("Vowels =", count)

 

Program 5 – Count Characters

text = input("Enter String: ")

 

print("Characters =", len(text))

 

Common Errors

  1. IndexError

text = "Python"

 

print(text[20])

❌ Error क्योंकि Index उपलब्ध नहीं है।

 

  1. TypeError

name = "Python"

 

name[0] = "J"

❌ क्योंकि String Immutable है।

 

Best Practices

  • हमेशा f-string का उपयोग करें।
  • String को Modify करने के लिए नई String बनाएँ।
  • User Input पर Validation करें।
  • सही Index Range का उपयोग करें।
  • बड़े Projects में Unicode Strings का ध्यान रखें।

 

परीक्षा की दृष्टि से महत्वपूर्ण तथ्य

  • String एक Immutable Sequence Data Type है।
  • Positive Indexing 0 से शुरू होती है।
  • Negative Indexing -1 से शुरू होती है।
  • len() String की Length बताता है।
  • + से Concatenation और * से Repetition होती है।
  • [::-1] से String Reverse की जाती है।
  • in और not in Membership Operators हैं।

 

Frequently Asked Questions (FAQs)

प्रश्न 1. String क्या है?

Characters का क्रम (Sequence) जो Quotes के अंदर लिखा जाता है।

प्रश्न 2. Python में String Mutable है या Immutable?

Immutable।

प्रश्न 3. String की Length कैसे ज्ञात करते हैं?

len() Function से।

प्रश्न 4. String Reverse कैसे करते हैं?

[::-1] Slicing द्वारा।

प्रश्न 5. सबसे अच्छा String Formatting तरीका कौन-सा है?

f-string।

 

Quick Revision

  • String = Characters का Sequence
  • Quotes = ' ', " ", ''' '''
  • Indexing = Positive और Negative
  • Slicing = string[start:stop:step]
  • Immutable = Direct Modification संभव नहीं
  • len() = Length
  • + = Concatenation
  • * = Repetition
  • [::-1] = Reverse String
  • f-string = आधुनिक Formatting

 

अगला अध्याय

Python String Methods (40+ Methods Complete Guide) — इसमें upper(), lower(), capitalize(), title(), strip(), replace(), find(), split(), join(), count(), startswith(), endswith() सहित सभी String Methods को एक ही विस्तृत Article में उदाहरण, Practical Programs और Interview Questions सहित समझेंगे।

यह अध्याय आपकी "Ekta Computer Notes Series" का सबसे महत्वपूर्ण Python String Reference Guide होगा।

 

निष्कर्ष (Conclusion)

Python Strings की पूरी जानकारी हिंदी में। String क्या है, String Creation, Indexing, Slicing, String Operators, Built-in Functions, 40+ String Methods, Formatting और Practical Programs उदाहरण सहित सीखें।