String Functions in Python :-
1. count()
count() method string में किसी character या substring के occurrences (कितनी बार आया है) की संख्या बताता है।
English: The count() method returns the number of non-overlapping occurrences of a substring in a string.
Syntax:
string.count(substring)
Example:
text = "banana"
print(text.count("a"))
print(text.count("an"))
Output:
3
2
2. find()
find() method string में किसी substring की पहली occurrence का index return करता है। यदि substring नहीं मिलती, तो -1 return करता है।
English: The find() method returns the index of the first occurrence of a substring. It returns -1 if the substring is not found.
Syntax:
string.find(substring)
Example:
text = "Hello Python"
print(text.find("Python"))
print(text.find("Java"))
Output:
6
-1
3. rfind()
rfind() method string में किसी substring की अंतिम occurrence का index return करता है। यदि substring नहीं मिलती, तो -1 return करता है।
English: The rfind() method returns the highest index where a substring is found. It returns -1 if the substring is not found.
Example:
text = "banana"
print(text.rfind("a"))
print(text.rfind("na"))
Output:
5
4
4. capitalize()
Hindi: capitalize() method string के पहले character को uppercase और बाकी characters को lowercase करता है।
English: The capitalize() method converts the first character to uppercase and the remaining characters to lowercase.
Example:
text = "hELLO PYTHON"
print(text.capitalize())
Output:
Hello python
5. title()
Hindi: title() method प्रत्येक शब्द के पहले character को uppercase और बाकी characters को lowercase करता है।
English: The title() method converts the first character of each word to uppercase and the remaining characters to lowercase.
Example:
text = "welcome to python"
print(text.title())
Output:
Welcome To Python
6. lower()
lower() method string के सभी uppercase letters को lowercase में बदलता है।
English: The lower() method converts all uppercase letters in a string to lowercase.
Example:
text = "HELLO PYTHON"
print(text.lower())
Output:
hello python
7. upper()
upper() method string के सभी lowercase letters को uppercase में बदलता है।
English: The upper() method converts all lowercase letters in a string to uppercase.
Example:
text = "Hello Python"
print(text.upper())
Output:
HELLO PYTHON
8. swapcase()
Hindi: swapcase() method uppercase letters को lowercase और lowercase letters को uppercase में बदलता है।
English: The swapcase() method converts uppercase letters to lowercase and lowercase letters to uppercase.
Example:
text = "Hello PYTHON"
print(text.swapcase())
Output:
hELLO python
9. islower()
Hindi: islower() method True return करता है, यदि string में कम-से-कम एक cased character हो और सभी cased characters lowercase हों।
English: The islower() method returns True if the string contains at least one cased character and all cased characters are lowercase.
Example:
print("hello".islower())
print("Hello".islower())
print("123".islower())
Output:
True
False
False
10. isupper()
Hindi: isupper() method True return करता है, यदि string में कम-से-कम एक cased character हो और सभी cased characters uppercase हों।
English: The isupper() method returns True if the string contains at least one cased character and all cased characters are uppercase.
Example:
print("HELLO".isupper())
print("Hello".isupper())
print("123".isupper())
Output:
True
False
False
11. istitle()
Hindi: istitle() method जाँचता है कि string title case में है या नहीं।
English: The istitle() method checks whether a string follows title-case rules.
Example:
print("Hello World".istitle())
print("Hello world".istitle())
print("hello world".istitle())
Output:
True
False
False
12. replace()
Hindi: replace() method string में किसी पुराने substring को नए substring से बदलता है।
English: The replace() method replaces occurrences of an old substring with a new substring.
Syntax:
string.replace(old, new)
Example:
text = "I like Java"
print(text.replace("Java", "Python"))
Output:
I like Python
Note: Original string नहीं बदलती; method एक नई string return करता है।
13. strip()
Hindi: strip() method string के दोनों सिरों से whitespace हटाता है। इसमें spaces, tabs और newline characters शामिल हो सकते हैं।
English: The strip() method removes leading and trailing whitespace from a string.
Example:
text = " Hello Python "
print(text.strip())
Output:
Hello Python
14. lstrip()
Hindi: lstrip() method string के बाईं ओर (शुरुआत में) मौजूद whitespace हटाता है।
English: The lstrip() method removes leading whitespace from a string.
Example:
text = " Hello Python "
print(text.lstrip())
Output:
Hello Python
15. rstrip()
Hindi: rstrip() method string के दाईं ओर (अंत में) मौजूद whitespace हटाता है।
English: The rstrip() method removes trailing whitespace from a string.
Example:
text = " Hello Python "
print(text.rstrip())
Output:
Hello Python
16. split()
Hindi: split() method string को separator के आधार पर अलग-अलग भागों में बाँटकर एक list return करता है।
English: The split() method divides a string using a separator and returns a list of substrings.
Syntax:
string.split(separator)
Example:
text = "Python Java HTML"
print(text.split())
Output:
['Python', 'Java', 'HTML']
Example with a separator:
text = "apple,banana,mango"
print(text.split(","))
Output:
['apple', 'banana', 'mango']
17. partition()
Hindi: partition() method string को पहली occurrence of separator के आधार पर तीन भागों में बाँटता है और tuple return करता है।
English: The partition() method splits a string at the first occurrence of a separator and returns a tuple containing three parts.
Example:
text = "Python is easy"
print(text.partition("is"))
Output:
('Python ', 'is', ' easy')
तीन भाग हैं:
Separator से पहले का भाग।
Separator स्वयं।
Separator के बाद का भाग।
यदि separator नहीं मिलता, तो पूरी string पहले भाग में और बाकी दोनों भागों में empty strings मिलती हैं।
18. join()
Hindi: join() method किसी iterable के strings को एक separator के साथ जोड़कर एक string बनाता है।
English: The join() method combines strings from an iterable into one string using the specified separator.
Syntax:
separator.join(iterable)
Example:
words = ["Python", "is", "easy"]
print(" ".join(words))
print("-".join(words))
Output:
Python is easy
Python-is-easy
Important: Iterable के सभी elements strings होने चाहिए।
19. isspace()
Hindi: isspace() method True return करता है, यदि string में कम-से-कम एक character हो और सभी characters whitespace हों।
English: The isspace() method returns True if the string is non-empty and all its characters are whitespace.
Example:
print(" ".isspace())
print("Hello".isspace())
print("".isspace())
Output:
True
False
False
20. isalpha()
Hindi: isalpha() method True return करता है, यदि string में कम-से-कम एक character हो और सभी characters alphabetic हों।
English: The isalpha() method returns True if the string is non-empty and all its characters are alphabetic.
Example:
print("Python".isalpha())
print("Python123".isalpha())
print("Hello World".isalpha())
Output:
True
False
False
Note: बीच में space होने पर "Hello World".isalpha() का result False होगा।
21. isdigit()
Hindi: isdigit() method True return करता है, यदि string में कम-से-कम एक character हो और सभी characters digit characters हों।
English: The isdigit() method returns True if the string is non-empty and all its characters are digits.
Example:
print("12345".isdigit())
print("12.5".isdigit())
print("Python123".isdigit())
Output:
True
False
False
Note: isdigit() सभी numeric-looking strings को स्वीकार नहीं करता। उदाहरण के लिए "-123" और "12.5" के लिए result False होगा।
22. isalnum()
Hindi: isalnum() method True return करता है, यदि string में कम-से-कम एक character हो और सभी characters alphabetic या numeric हों। इसमें space या punctuation नहीं होना चाहिए।
English: The isalnum() method returns True if the string is non-empty and all its characters are alphanumeric.
Example:
print("Python123".isalnum())
print("Python".isalnum())
print("Python 123".isalnum())
print("12345".isalnum())
Output:
True
True
False
True
23. startswith()
Hindi: startswith() method जाँचता है कि string किसी दिए गए prefix से शुरू होती है या नहीं। यह True या False return करता है।
English: The startswith() method checks whether a string begins with a specified prefix.
Example:
text = "Python Programming"
print(text.startswith("Python"))
print(text.startswith("Java"))
Output:
True
False
24. endswith()
Hindi: endswith() method जाँचता है कि string किसी दिए गए suffix पर समाप्त होती है या नहीं।
English: The endswith() method checks whether a string ends with a specified suffix.
Example:
filename = "notes.pdf"
print(filename.endswith(".pdf"))
print(filename.endswith(".txt"))
Output:
True
False
25. encode()
Hindi: encode() method string को bytes में बदलता है। इसमें सामान्यतः UTF-8 जैसे encoding format का उपयोग किया जाता है।
English: The encode() method converts a string into bytes using a specified character encoding, such as UTF-8.
Syntax:
string.encode(encoding="utf-8")
Example:
text = "Hello"
result = text.encode("utf-8")
print(result)
print(type(result))
Output:
b'Hello'
<class 'bytes'>
Explanation: encode() string को bytes object में बदलता है। Unicode text को bytes में बदलते समय UTF-8 एक सामान्य विकल्प है।
26. decode()
Hindi: decode() method bytes object को दिए गए encoding format के आधार पर string में बदलता है।
English: The decode() method converts a bytes object into a string using the specified character encoding.
Syntax:
bytes_data.decode(encoding="utf-8")
Example:
data = b"Hello"
result = data.decode("utf-8")
print(result)
print(type(result))
Output:
Hello
<class 'str'>
Important: decode() Python str का method नहीं है; यह bytes और bytearray पर उपयोग होता है। इसलिए "Hello".decode() लिखने पर error आएगा।