Strings को manipulate करने और उनके साथ काम करने के लिए Python कई methods प्रदान करता है। String methods बहुत ही उपयोगी होती हैं और इनका उपयोग text को process करने के लिए किया जाता है। नीचे आपको कुछ strings methods दिए गए हैं:-
1. lower()
यह मेथड string के सभी characters को lowercase में बदल देता है।
Syntax:
string.lower()
Example:
text = "Hello, World!"
print(text.lower())
# Output: hello, world!
2. upper()
यह मेथड string के सभी characters को uppercase में बदल देता है।
Syntax:
string.upper()
Example:
text = "Hello, World!"
print(text.upper())
# Output: HELLO, WORLD!
3. strip()
यह मेथड string के starting और ending से extra spaces या अनचाहे characters को remove कर देता है।
Syntax:
string.strip([chars])
Example:
text = " Hello, World! "
print(text.strip())
# Output: Hello, World!
4. replace()
यह मेथड string में एक character या substring को दूसरे character या substring से बदल देता है।
Syntax:
string.replace(old, new, [count])
Example:
text = "Hello, World!"
print(text.replace("World", "Python"))
# Output: Hello, Python!
5. split()
यह मेथड string को एक विशेष delimiter (जैसे space, comma आदि) के आधार पर parts में divide करता है और एक list को return करता है।
Syntax:
string.split([separator], [maxsplit])
Example:
text = "apple,banana,cherry"
print(text.split(","))
# Output: ['apple', 'banana', 'cherry']
6. join()
यह मेथड iterable (जैसे list, tuple) के elements को एक string में जोड़ता है।
Syntax:
separator.join(iterable)
Example:
fruits = ["apple", "banana", "cherry"]
print(", ".join(fruits))
# Output: apple, banana, cherry
7. find()
यह मेथड string में दिए गए substring को ढूंढकर उसकी पहली position का index बताती है। अगर substring नहीं मिलता है, तो यह -1 return करती है।
Syntax:
string.find(substring, [start], [end])
Example:
text = "Hello, World!"
print(text.find("World"))
# Output: 7
8. isalpha()
यह मेथड check करता है कि string के सभी characters alphabets हैं या नहीं।
Syntax:
string.isalpha()
Example:
text = "Python"
print(text.isalpha())
# Output: True
9. isnumeric()
यह मेथड check करता है कि string के सभी characters नंबर हैं या नहीं।
Syntax:
string.isnumeric()
Example:
text = "12345"
print(text.isnumeric())
# Output: True
10. capitalize()
यह मेथड string के पहले character को uppercase और बाकी characters को lowercase में बदल देता है।
Syntax:
string.capitalize()
Example:
text = "hello, world!"
print(text.capitalize())
# Output: Hello, world!
इसे पढ़ें:-