Python Strings का परिचय
In this page:
variable = "text"
variable = 'text'
variable = """multi-line
text"""
Strings बनाना
String characters की एक ordered sequence है, और Python आपको इसे single quotes, double quotes, या triple quotes से बनाने देता है -- ये तीनों बिल्कुल एक जैसा str type produce करते हैं, तो चुनाव सुविधा पर निर्भर करता है, जैसे किसी apostrophe को escape करने से बचना।
उदाहरण: Creating Strings
s1 = 'single'
s2 = "double"
s3 = """triple"""
print(type(s1), type(s2), type(s3)) # all three are the same str type
Characters तक पहुँचना
Square-bracket indexing, जैसे text[0], उस zero-based position पर एक character निकालता है; Python negative indices भी support करता है, तो text[-1] पहले string की length जाने बिना आख़िरी character निकाल लेता है।
उदाहरण: Accessing Characters
text = "Python"
print(text[0]) # first character, index 0
print(text[-1]) # last character, negative indexing counts from the end
String की Length
len(text) किसी string का पूरा character count देता है, spaces और punctuation समेत -- यह value आप input की length validate करते समय या index-based loop से iterate करते समय बार-बार इस्तेमाल करेंगे।
उदाहरण: String Length
text = "Hello, World!"
print(len(text))
Multiline Strings
Triple-quoted strings ("""...""") जैसे type की गई हैं वैसे ही कई lines में फैल सकती हैं, line breaks को बनाए रखते हुए -- यह formatted text के किसी block, docstring, या multi-line message को बिना manual \n characters के embed करने का standard तरीका है।
उदाहरण: Multiline Strings
message = """Line one
Line two
Line three""" # triple quotes preserve the line breaks as typed
print(message)
Strings की Immutability
एक बार बनने के बाद, Python string को कभी in-place modify नहीं किया जा सकता -- .upper() या .replace() जैसे operations original string को नहीं बदलते, वे एक बिल्कुल नई string return करते हैं, और original को memory में जस का तस छोड़ देते हैं।
उदाहरण: Immutability of Strings
text = "hello"
upper_text = text.upper() # returns a new string, doesn't modify text
print(text) # still lowercase
print(upper_text)
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: