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

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

The Python Interpreter पायथन इंटरप्रेटर

The Python interpreter is a program that executes Python code. It reads and interprets Python scripts or interactive commands, converting them into machine-readable bytecode for the computer to execute.The interpreter is a crucial component for running Python code, facilitating both learning and development processes. Users can interact with the interpreter in two primary modes: पायथन इंटरप्रेटर एक प्रोग्राम है जो पायथन कोड को निष्पादित करता है। यह पायथन स्क्रिप्ट या इंटरैक्टिव कमांड को पढ़ता है और व्याख्या करता है, उन्हें कंप्यूटर द्वारा निष्पादित करने के लिए मशीन-पठनीय बाइटकोड में परिवर्तित करता है। दुभाषिया पायथन कोड चलाने के लिए एक महत्वपूर्ण घटक है, जो सीखने और विकास दोनों प्रक्रियाओं को सुविधाजनक बनाता है। उपयोगकर्ता इंटरप्रेटर के साथ दो प्राथमिक मोड में बातचीत कर सकते हैं:- 1. Interactive Mode:- In this mode we can launch the Python interpreter in our terminal or command prompt without specifying a script.This mode allows us to enter Python commands and see immediate results...

Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह

Flow of execution represents a general outline of Python program execution. The specific flow can vary based on the program's structure, control flow statements (if, else, for, while), and function calls. निष्पादन का प्रवाह पायथन प्रोग्राम निष्पादन की एक सामान्य रूपरेखा का प्रतिनिधित्व करता है। विशिष्ट प्रवाह प्रोग्राम की संरचना, नियंत्रण प्रवाह विवरण (यदि, अन्यथा, के लिए, जबकि), और फ़ंक्शन कॉल के आधार पर भिन्न हो सकता है। 1.) Start: The program begins execution. 1.) प्रारंभ: प्रोग्राम का निष्पादन प्रारंभ होता है। 2.) Import Modules: If necessary, the program imports external modules or libraries. 2.) मॉड्यूल आयात करें: यदि आवश्यक हो, तो प्रोग्राम बाहरी मॉड्यूल या लाइब्रेरी आयात करता है। 3.) Define Functions: Any functions used in the program are defined. 3.) फंक्शन्स को परिभाषित करें: प्रोग्राम में उपयोग किए गए किसी भी फंक्शन को परिभाषित किया जाता है। 4.) Main Program: The main body of the program starts. 4.) मुख्य प्रोग्राम: प्रोग्राम का मुख्य भाग प्रारंभ होता है। 5.) Initializat...