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

Inbuilt Functions in Python पाइथन में अंतर्निहित फ़ंक्शन्स

Python is a powerful programming language that provides many inbuilt functions to make coding simple and efficient. These functions help programmers perform common tasks like finding length, converting characters, or checking memory addresses without writing extra code. पायथन एक शक्तिशाली प्रोग्रामिंग भाषा है जो कई अंतर्निहित (inbuilt) फ़ंक्शन्स प्रदान करती है, जिससे कोडिंग आसान और प्रभावी हो जाती है। ये फ़ंक्शन प्रोग्रामर को सामान्य कार्य करने में मदद करते हैं जैसे लंबाई निकालना, अक्षरों का रूपांतरण करना या मेमोरी एड्रेस जांचना, और इसके लिए अतिरिक्त कोड लिखने की आवश्यकता नहीं होती। (a)  id()  – Unique Identifier of an Object  किसी वस्तु का अद्वितीय पहचानकर्ता The  id()  function returns the unique memory address of an object. Every variable or object in Python is stored in memory, and  id()  helps us know where exactly it is stored. id()  फ़ंक्शन किसी ऑब्जेक्ट का अद्वितीय मेमोरी एड्रेस लौटाता है। पायथन में हर वेरिएबल या ऑब्जेक्ट मेमोरी में संग्रह...