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

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...

Filter function of python

filter function:-  Imagine we have a bunch of things, like a list of numbers, a collection of fruits, or any other group of items. Now, let's say we want to pick out only certain items from that group based on a specific condition. This is where the filter function comes in handy. Let's say we have a list of numbers, and we want to filter out only the even numbers. We define a condition-checking function that checks if a number is even. Then, we pass this function and the list of numbers to the filter function. It goes through each number, applies the condition-checking function, and keeps only the numbers that are even. Finally, it gives us a new list containing only the even numbers. def is_even(num):     return num % 2 == 0 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filtered_numbers = list(filter(is_even, numbers)) print(filtered_numbers) Output [2, 4, 6, 8, 10] We can also utilize lambda expressions to define filtering conditions directly within the filter function. nu...

Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं

Python is a versatile and popular programming language known for its simplicity and readability. It has a wide range of features that make it suitable for various types of projects. Here are some key features of the Python programming language:- पायथन एक बहुमुखी और लोकप्रिय प्रोग्रामिंग भाषा है जो अपनी सरलता और पठनीयता के लिए जानी जाती है। इसमें कई प्रकार की विशेषताएं हैं जो इसे विभिन्न प्रकार की परियोजनाओं के लिए उपयुक्त बनाती हैं। पायथन प्रोग्रामिंग भाषा की कुछ प्रमुख विशेषताएं निम्न हैं:- 1. Open Source ओपन सोर्स:-   Python is an open-source language, which encourages collaboration and allows developers to contribute to its ongoing development. पायथन एक ओपन-सोर्स भाषा है, जो सहयोग को प्रोत्साहित करती है और डेवलपर्स को इसके चल रहे विकास में योगदान करने की अनुमति देती है। 2. Integrated Development & Learning Environments एकीकृत विकास एवं अध्ययन वातावरण  (IDLEs/IDEs):-  Python has several powerful IDLEs/IDEs, such as PyCharm, VSCode, and Jupyter, that provide too...