← Back to Python Course | Chapter 5: Strings | Lesson 1 of 6

Python Strings का परिचय

String text का एक टुकड़ा है, जैसे letter-मोतियों की एक माला। आप हर मोती को उसकी position से देख सकते हैं और गिन सकते हैं कि कितने हैं।
Syntax
python
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

python
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

python
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

python
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

python
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

python
text = "hello"
upper_text = text.upper()  # returns a new string, doesn't modify text
print(text)  # still lowercase
print(upper_text)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.