Python String क्या है? String Methods, Indexing & Slicing in Hindi | RBSE Class 12

इस लेख में: Python String क्या है, String कैसे बनाते हैं, Indexing, Negative Indexing, Slicing, Concatenation, Repetition तथा upper(), lower(), strip(), replace(), split(), join(), find(), count(), startswith(), endswith() और String Formatting जैसे important concepts को आसान Hindi में examples के साथ समझेंगे।

Python String क्या है?

String Python में characters का sequence होता है। String का उपयोग text या characters को store करने के लिए किया जाता है।

Python में String को Single Quotes ' ', Double Quotes " " या Triple Quotes के अंदर लिखा जा सकता है।

Example

name = "Rahul"

print(name)

Output:

Rahul

Python में String बनाने के तरीके

Single Quotes

name = 'Rahul'

print(name)

Double Quotes

name = "Rahul"

print(name)

Triple Quotes

message = """Python is easy to learn."""

print(message)
Important: Python में Single Quotes और Double Quotes दोनों से String बनाई जा सकती है।

String की मुख्य विशेषताएँ

  • String characters का sequence है।
  • String को quotes के अंदर लिखा जाता है।
  • String में indexing उपलब्ध है।
  • String में slicing उपलब्ध है।
  • String immutable होती है।
  • String पर कई built-in methods उपलब्ध हैं।

String Indexing

String के प्रत्येक character का एक index number होता है। Indexing हमेशा 0 से शुरू होती है।

name = "Python"

print(name[0])
print(name[1])
print(name[2])

Output:

P
y
t

String Index Position

Python

Index:
 P  y  t  h  o  n
 0  1  2  3  4  5

इसलिए name[0] पहला character और name[5] अंतिम character प्राप्त करेगा।

Negative Indexing

Python में String को पीछे से access करने के लिए negative indexing का उपयोग किया जाता है।

name = "Python"

print(name[-1])
print(name[-2])
print(name[-3])

Output:

n
o
h

Negative Index Table

Python

 P   y   t   h   o   n
-6  -5  -4  -3  -2  -1

String Slicing

String के किसी particular portion को प्राप्त करने के लिए slicing का उपयोग किया जाता है।

Syntax:

string[start:stop]

यहाँ start शामिल होता है लेकिन stop index शामिल नहीं होता।

Example

text = "Python"

print(text[0:3])

Output:

Pyt

String Slicing with Step

Syntax:

string[start:stop:step]
text = "Python"

print(text[0:6:2])

Output:

Pto

String को Reverse करना

Negative step का उपयोग करके String को reverse किया जा सकता है।

text = "Python"

print(text[::-1])

Output:

nohtyP
Exam Tip: string[::-1] String को reverse करने का commonly used तरीका है।

String Concatenation

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

first_name = "Rahul"
last_name = "Kumar"

full_name = first_name + " " + last_name

print(full_name)

Output:

Rahul Kumar

String Repetition

String को कई बार repeat करने के लिए * operator का उपयोग किया जा सकता है।

text = "Python "

print(text * 3)

Output:

Python Python Python 

String की Length — len()

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

text = "Python"

print(len(text))

Output:

6

upper() Method

upper() String के सभी alphabetic characters को uppercase में बदलता है।

text = "python programming"

print(text.upper())

Output:

PYTHON PROGRAMMING

lower() Method

lower() String के alphabetic characters को lowercase में बदलता है।

text = "PYTHON PROGRAMMING"

print(text.lower())

Output:

python programming

capitalize() Method

capitalize() String के पहले character को uppercase और बाकी alphabetic characters को lowercase करता है।

text = "python programming"

print(text.capitalize())

Output:

Python programming

title() Method

title() प्रत्येक word के first character को uppercase करने में उपयोगी है।

text = "python programming language"

print(text.title())

Output:

Python Programming Language

strip() Method

strip() String के शुरुआत और अंत में मौजूद whitespace को हटाने के लिए उपयोग किया जाता है।

text = "   Python   "

print(text.strip())

Output:

Python

lstrip() Method

lstrip() String के left side की whitespace हटाता है।

text = "   Python"

print(text.lstrip())

rstrip() Method

rstrip() String के right side की whitespace हटाता है।

text = "Python   "

print(text.rstrip())

replace() Method

replace() String के किसी text को दूसरे text से replace करने के लिए उपयोग किया जाता है।

text = "I like Java"

new_text = text.replace("Java", "Python")

print(new_text)

Output:

I like Python

split() Method

split() String को अलग-अलग parts में divide करके एक List return करता है।

text = "Python is easy"

words = text.split()

print(words)

Output:

['Python', 'is', 'easy']

join() Method

join() iterable के elements को एक String में जोड़ने के लिए उपयोग किया जाता है।

words = ["Python", "is", "easy"]

text = " ".join(words)

print(text)

Output:

Python is easy

find() Method

find() String में किसी substring की पहली occurrence का index return करता है। यदि substring नहीं मिलती तो -1 return किया जाता है।

text = "Python Programming"

print(text.find("Program"))

count() Method

count() किसी substring या character की occurrences की संख्या बताता है।

text = "banana"

print(text.count("a"))

Output:

3

startswith() Method

startswith() check करता है कि String किसी specified text से शुरू होती है या नहीं।

text = "Python Programming"

print(text.startswith("Python"))

Output:

True

endswith() Method

endswith() check करता है कि String किसी specified text पर समाप्त होती है या नहीं।

text = "Python Programming"

print(text.endswith("Programming"))

Output:

True

isalpha() Method

isalpha() check करता है कि String में केवल alphabetic characters हैं या नहीं।

text = "Python"

print(text.isalpha())

Output:

True

isdigit() Method

isdigit() check करता है कि String में केवल digits हैं या नहीं।

text = "12345"

print(text.isdigit())

Output:

True

isalnum() Method

isalnum() check करता है कि String के सभी characters alphanumeric हैं या नहीं।

text = "Python123"

print(text.isalnum())

Output:

True

String Membership — in Operator

in operator से check कर सकते हैं कि कोई character या substring String में मौजूद है या नहीं।

text = "Python Programming"

print("Python" in text)
print("Java" in text)

Output:

True
False

String Formatting

Python में variables को String के अंदर insert करने के कई तरीके हैं। Modern Python में f-string बहुत उपयोगी है।

name = "Rahul"
age = 18

message = f"My name is {name} and I am {age} years old."

print(message)

Output:

My name is Rahul and I am 18 years old.

format() Method

format() method की सहायता से String में values insert की जा सकती हैं।

name = "Rahul"
age = 18

message = "My name is {} and I am {} years old.".format(name, age)

print(message)

String Immutable क्यों है?

Python में String immutable होती है। इसका अर्थ है कि existing String के किसी individual character को सीधे modify नहीं किया जा सकता।

text = "Python"

# text[0] = "J"

यदि String में परिवर्तन करना हो तो नई String create करनी पड़ती है।

text = "Python"

text = "J" + text[1:]

print(text)

Output:

Jython
Exam Tip: String is immutable Python का बहुत important concept है।

Escape Characters

String में special characters लिखने के लिए escape sequences का उपयोग किया जाता है।

New Line — \n

print("Hello\nPython")

Output:

Hello
Python

Tab — \t

print("Hello\tPython")

Double Quote — \"

print("He said \"Hello\"")

Important Python String Methods

Method उपयोग
upper() Uppercase में बदलना
lower() Lowercase में बदलना
capitalize() पहले character को uppercase करना
title() हर word का पहला character uppercase करना
strip() Beginning और ending whitespace हटाना
lstrip() Left side की whitespace हटाना
rstrip() Right side की whitespace हटाना
replace() Text replace करना
split() String को List में split करना
join() Elements को String में जोड़ना
find() Substring का index खोजना
count() Occurrences count करना
startswith() Starting text check करना
endswith() Ending text check करना
isalpha() Alphabetic characters check करना
isdigit() Digits check करना
isalnum() Alphanumeric characters check करना

String Quick Revision

Concept मुख्य बात
String Characters का sequence
Indexing 0 से शुरू होती है
Negative Index -1 से शुरू होता है
Slicing string[start:stop:step]
Concatenation + operator
Repetition * operator
Length len()
Mutable / Immutable String immutable है
Uppercase upper()
Lowercase lower()
Replace replace()
Split split()
Join join()

RBSE Class 12 Important Questions

  1. Python String क्या है?
  2. String की मुख्य विशेषताएँ लिखिए।
  3. Python में String कैसे बनाते हैं?
  4. String Indexing क्या है?
  5. Negative Indexing क्या है?
  6. String Slicing क्या है?
  7. String को reverse कैसे करते हैं?
  8. String Concatenation क्या है?
  9. String Repetition क्या है?
  10. len() function का उपयोग क्या है?
  11. upper() और lower() में अंतर लिखिए।
  12. strip() method क्या करता है?
  13. replace() method का उपयोग बताइए।
  14. split() method क्या करता है?
  15. join() method का उपयोग बताइए।
  16. find() method क्या करता है?
  17. count() method क्या करता है?
  18. startswith() और endswith() क्या हैं?
  19. String immutable क्यों कहलाती है?
  20. f-string क्या है?

Frequently Asked Questions (FAQs)

Python String क्या है?

Python String characters का sequence है जिसका उपयोग text और characters को store करने के लिए किया जाता है।

Python में String Indexing कहाँ से शुरू होती है?

Python में positive String indexing 0 से शुरू होती है और negative indexing -1 से शुरू होती है।

क्या Python String mutable है?

नहीं। Python String immutable होती है, इसलिए existing String के individual character को सीधे modify नहीं किया जा सकता।

String को reverse कैसे करते हैं?

String को reverse करने के लिए slicing में [::-1] का उपयोग किया जा सकता है।

split() और join() में क्या अंतर है?

split() String को अलग-अलग parts में divide करके List देता है, जबकि join() elements को जोड़कर एक String बनाता है।

Python में String की length कैसे पता करते हैं?

String की length पता करने के लिए len() function का उपयोग किया जाता है।

अब अगला Topic पढ़ें

RBSE Python Study Tip: String के लिए सबसे पहले Indexing और Slicing अच्छी तरह समझें। इसके बाद upper(), lower(), strip(), replace(), split(), join(), find() और count() की practice करें। Exam में छोटे programs और output-based questions के लिए इन methods की practice बहुत उपयोगी रहेगी।
Official Reference:

Python String की विस्तृत जानकारी के लिए Python की official documentation देखें।

Python String Documentation