Python String in Hindi – पायथन में String क्या है?

Python में String एक प्रकार का डेटा टाइप है जो text को represent (प्रस्तुत) करता है। यह characters का एक sequence (क्रम) होता है। जिसे single quotes (‘ ‘) या double quotes (” “) के अंदर लिखा जाता है।

String का इस्तेमाल alphabets, numbers, special characters और white space को एक साथ स्टोर करने के लिए इस्तेमाल होते हैं।

String को Create करना

Python में एक string बनाने के लिए, आपको इसे quotes के अंदर रखना होता है। आप double या single quotes का उपयोग कर सकते हैं।

# Single quotes का उपयोग
string1 = 'Hello'

# Double quotes का उपयोग
string2 = "World"

# Triple quotes का उपयोग (Multi-line string के लिए)
string3 = '''Hello,
This is a multi-line string.'''

String की लंबाई (Length of a String)

Python में len() फंक्शन का उपयोग करके आप किसी भी स्ट्रिंग की लंबाई (number of characters) प्राप्त कर सकते हैं।

string = "Hello"

print(len(string))  # Output: 5

String को Access करना (Accessing a String)

आप string के किसी भी character को उसके index number से एक्सेस कर सकते हैं। याद रखें कि Python में indexing 0 से शुरू होती है।

string = "Python"

print(string[0])  # Output: P

print(string[1])  # Output: y

String का Slicing (String Slicing)

String Slicing का मतलब है कि आप string से एक निश्चित हिस्सा (fixed part) निकाल सकते हैं। इसके लिए आप : (colon) का उपयोग करते हैं।

string = "Programming"

print(string[0:5])  

# Output: Progr

यहां 0:5 का मतलब है कि हम index 0 से लेकर 4 तक के characters निकाल रहे हैं।

String Concatenation (String जोड़ना)

आप दो या दो से अधिक strings को जोड़ने के लिए + ऑपरेटर का उपयोग कर सकते हैं।

string1 = "Hello"

string2 = "Python"

result = string1 + " " + string2

print(result)  

# Output: Hello Python

String Repetition (String को बार-बार दोहराना)

आप string को बार-बार दोहराने के लिए * ऑपरेटर का उपयोग कर सकते हैं।

string = "Python "

print(string * 3)  

# Output: Python Python Python

String Formatting (String को फॉर्मैट करना)

Python में string formatting के लिए कई तरीके हैं। एक तरीका है f-strings का उपयोग करना।

name = "Yugal"

age = 30

greeting = f"Hello, my name is {name} and I am {age} years old."

print(greeting)  

# Output: Hello, my name is Yugal and I am 30 years old.

Escape Characters 

अगर आपको string में special characters जैसे newline (\n) या tab (\t) का उपयोग करना हो तो आपको escape characters का इस्तेमाल करना होता है।

string = "Hello\nWorld"

print(string)

इससे आउटपुट इस प्रकार होगा:

Hello

World

Multiline Strings 

आप multiline strings भी बना सकते हैं। इसके लिए तीन double quotes (“””) या single quotes (”’) का उपयोग किया जाता है।

string = """This is

a multiline

string."""

print(string)

इससे आउटपुट होगा:

This is

a multiline

string.

इसे पढ़ें:-

Leave a Comment