Skip to main content

String in Python पायथन में स्ट्रिंग, String Operations स्ट्रिंग ऑपरेशन, String Functions and Methods स्ट्रिंग फ़ंक्शन और विधियाँ

In Python, a string is an immutable sequence of characters. Strings are enclosed in either single quotes (`'`) or double quotes (`"`), and can also be enclosed in triple quotes for multi-line strings.

एक स्ट्रिंग वर्णों का एक अपरिवर्तनीय अनुक्रम है। स्ट्रिंग्स सिंगल कोट्स (` `) या डबल कोट्स (`` ``) में संलग्न होती हैं, और मल्टी-लाइन स्ट्रिंग्स को ट्रिपल कोट्स ('''''') में संलग्न किया जा सकता है।

Creating Strings स्ट्रिंग तैयार करना :-

# Single and double-quoted strings

s1 = 'Hello'

s2 = "World"

# Multi-line string with triple single and triple double quote:-

s3 = '''This is

a multi-line

string.'''

s4 = """This is also

a multi-line

string."""

String Operations स्ट्रिंग ऑपरेशन :-

1. Concatenation संयोजन:- Combining two or more strings using the `+` operator.
`+` ऑपरेटर का उपयोग करके दो या दो से अधिक स्ट्रिंग्स का संयोजन किया जा सकता है।

s1 = 'Hello'
s2 = 'World'
s3 = s1 + '' + s2

Output:- 'Hello World'

2. Repetition दोहराव :- Repeating a string multiple times using the `*` operator.
`*` ऑपरेटर का उपयोग करके एक स्ट्रिंग को कई बार दोहराया जा सकता है।

s = 'Hello ' * 3
Output:- 'Hello Hello Hello '

3. Indexing अनुक्रमणिका :- Accessing individual characters using their index.
इंडेक्स का उपयोग करके स्ट्रिंग के एक-एक अक्षर तक पहुँचा जा सकता है।

s = 'Python'
print(s[0]) #Output:- 'P'
print(s[3]) #Output:- 'h'
print(s[-1]) #Output:- 'n'
print(s[-3]) #Output:- 'h'

4. Slicing स्लाइसिंग:- Extracting a substring by specifying the start and end indices.
प्रारंभ और अंत सूचकांक प्रदान कर एक स्ट्रिंग से सबस्ट्रिंग निकालना स्लाइसिंग कहलाता है।

s = 'Python'

print(s[1:4]) # Output:- 'yth'
print(s[:3]) # Output:- 'Pyt' (from start to index 2)
print(s[3:]) # Output:- 'hon' (from index 3 to the end)

5. String Length स्ट्रिंग की लंबाई:- Finding the number of characters in a string using `len()`.
`len()` का उपयोग करके स्ट्रिंग में वर्णों की संख्या ज्ञात की जा सकती हैं।

s = 'Hello'
print(len(s)) # Output:- 5

6. String Membership स्ट्रिंग सदस्यता:- Checking if a substring exists within a string using the `in` operator.
`इन` ऑपरेटर का उपयोग करके यह जांचना कि स्ट्रिंग के भीतर कोई सबस्ट्रिंग मौजूद है या नहीं।

s = 'Python'
print('th' in s) # Output: True
print('xy' in s) # Output: False

String Functions and Methods स्ट्रिंग फ़ंक्शन और विधियाँ:-

1.`upper()`:- Converts all characters to uppercase.
यह फंक्शन सभी वर्णों को अपरकेस में बदलता है।

s = 'hello'
print(s.upper())  # Output:- 'HELLO'

2. `lower()`:- Converts all characters to lowercase.
यह फंक्शन सभी वर्णों को लोअरकेस में बदलता है।

s = 'HELLO'
print(s.lower())  # Output: 'hello'

3.`strip()`:- Removes leading and trailing whitespace.
यह फंक्शन आरंभिक और अंतिम रिक्त स्थान हटाता है।

s = '  Hello  '
print(s.strip())  # Output:- 'Hello'

4.`replace()`:- Replaces all occurrences of a substring with another.
यह फंक्शन एक सबस्ट्रिंग की सभी घटनाओं को दूसरे से बदलता है।

s = 'Hello World'
print(s.replace('World', 'Python'))  
# Output:- 'Hello Python'

5. `split()`:- Splits a string into a list of substrings based on a delimiter.
यह फंक्शन एक स्ट्रिंग को सीमांक के आधार पर सबस्ट्रिंग की सूची में विभाजित करता है।

s = 'a,b,c'
print(s.split(','))  
# Output:- ['a', 'b', 'c']

6. `join()`:- Joins a list of strings into a single string using a specified separator.
यह फंक्शन निर्दिष्ट विभाजक का उपयोग करके स्ट्रिंग्स की सूची को एकल स्ट्रिंग में जोड़ता है।

lst = ['a', 'b', 'c']
print(','.join(lst))  # Output:- 'a,b,c'

7. `find()`:- Returns the index of the first occurrence of a substring, or `-1` if not found.
यह फंक्शन किसी सबस्ट्रिंग की पहली उपस्थिति का सूचकांक लौटाता है, या नहीं मिलने पर `-1` लौटाता है।

s = 'Hello'
print(s.find('o'))  # Output:- 4
print(s.find('z'))  # Output:- 1

8. `startswith()`:- Check if a string starts with a specific substring.
यह फंक्शन जांचता है कि क्या स्ट्रिंग किसी विशिष्ट सबस्ट्रिंग से शुरू होती है।

s = 'Hello World'
print(s.startswith('He'))  
# Output:- True
print(s.startswith('Wo'))  
# Output:- False

9. `endswith()`:- Check if a string ends with a specific substring.
यह फंक्शन जांचता है कि क्या स्ट्रिंग किसी विशिष्ट सबस्ट्रिंग से समाप्त होती है।

s = 'Hello World'
print(s.endswith('ld'))  
# Output:- True
print(s.endswith('He'))    
# Output:- False

Comments

Popular posts from this blog

Python IDLE पायथन आईडीएलई (इंटीग्रेटेड डेवलपमेंट एंड लर्निंग एनवायरनमेंट)

IDLE (Integrated Development and Learning Environment) is a graphical user interface (GUI) for the Python programming language. It includes the Python standard library and provides a convenient environment for writing, running, and debugging Python code. IDLE (इंटीग्रेटेड डेवलपमेंट एंड लर्निंग एनवायरनमेंट) पायथन प्रोग्रामिंग भाषा के लिए एक ग्राफिकल यूजर इंटरफेस (GUI) है। इसमें पायथन मानक लाइब्रेरी शामिल है और पायथन कोड लिखने, चलाने और डीबग करने के लिए एक सुविधाजनक वातावरण प्रदान करता है।  Key features of IDLE are:- आईडीएलई की मुख्य विशेषताएं हैं:-  1. Interactive Shell:- IDLE provides an interactive Python shell, allowing you to execute Python commands and see the immediate results. 1. इंटरएक्टिव शेल:- आईडीएलई एक इंटरैक्टिव पायथन शेल प्रदान करता है, जो आपको पायथन कमांड निष्पादित करने और तत्काल परिणाम देखने की अनुमति देता है।  2. Script Editor:- It includes a text editor for writing Python scripts. You can create, edit, and save Python files with a ".py" extension. 2. स्क्...

Operator's in Python पायथन में ऑपरेटर्स

Python supports various types of operators for performing operations on variables and values. Here are some common types of operators in Python: पायथन वेरिएबल्स और मानों पर संचालन करने के लिए विभिन्न प्रकार के ऑपरेटरों का समर्थन करता है। यहां पायथन में कुछ सामान्य प्रकार के ऑपरेटर हैं: 1. Arithmetic Operators:    + # Addition     - # Subtraction    * # Multiplication    / # Division    % # Modulus (remainder)    ** # Exponentiation    // # Floor Division 2. Comparison Operators:        == # Equal to    != # Not equal to    < # Less than    > # Greater than    <= # Less than or equal to    >= # Greater than or equal to     3. Logical Operators:       and # Logical AND    or # Logical OR    not # Logical NOT    4. Assignment Operators:    ...

Programming with Python Language Tutorial available in hindi and english पाइथन प्रोग्रामिंग टुटोरिअल हिंदी एवं अंग्रेजी भाषा में

 Unit -01 1. Introduction to python (पाइथन भाषा का परिचय) 2. History of Python Programming Language पाइथन प्रोग्रामिंग लैंग्वेज का इतिहास 3. Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं 4. Python’s Programming Language's Popularity पाइथन प्रोग्रामिंग लैंग्वेज की लोकप्रियता 5.The Future of Python Programming Language पायथन प्रोग्रामिंग लैंग्वेज का भविष्य 6. The Python Interpreter पायथन इंटरप्रेटर 7. The Python IDLE पाइथन आईडीएलई 8. Installation process of Python IDLE पायथन आईडीएलई की इंस्टालेशन प्रक्रिया 9. Dynamically typed and strongly typed features of python गतिशील रूप से टाइप और दृढ़ता से टाइप की गई भाषा पायथन 10. Basic Data Types of Python पायथन के बेसिक डेटा टाइप 11. Variables in python पायथन में वेरिएबल 12. Expressions in Python पायथन में अभिव्यक्तियां 13. Statements in Python पायथन में कथन 14. Operator's in Python पायथन में ऑपरेटर्स 15. Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह 16...