String Functions in Python

String Functions in Python

In Python, several built-in functions connected with the string data type. Using these functions we can easily modify and manipulate a string. In this section, we’ll go over several different functions that we can use to work with strings in Python 3.x.

पायथन में, कई बिल्ट-ईन फंक्शन स्ट्रिंग डेटा टाईप के साथ जुड़े हुए हैं। इन फंक्शन का उपयोग करके हम आसानी से एक स्ट्रिंग को संशोधित और जोड़ सकते हैं। इस खंड में, हम कई अलग-अलग कार्यों पर जाएंगे, जिनका उपयोग हम पायथन 3.x में स्ट्रिंग के साथ काम करने के लिए कर सकते हैं।

count()

count() function in an inbuilt function in python programming language that returns the number of occurrences of a sub-string in the given string.

पायथन प्रोग्रामिंग लैंग्वेज में एक इनबिल्ट फंक्शन में count() फंक्शन जो दिए गए स्ट्रिंग में एक स्ट्रिंग की घटनाओं की संख्या पर रिटर्न होता है।

Syntax: string.count (substring, start, end)

substring – string whose count is to be found.

सबस्ट्रिंग  – स्ट्रिंग जिसकी गिनती की जानी है

start (Optional) – starting index within the string where search starts.

स्टॉर्ट (ऑप्शनल) – इंडेक्स स्ट्रिंग के भीतर जहां सर्च शुरू होती है।

end (Optional) – ending index within the string where search ends.

इंड (ऑप्शनल) – इंडेक्स स्ट्रिंग के भीतर जहां सर्च समाप्त होती है।

Return Value: The count() method returns the number of times a specified value appears in the string.

रिटर्न वैल्यू: count() विधि एक निर्दिष्ट मान स्ट्रिंग में दिखाई देने की संख्या को रिटर्न करती है।

Example 1:
txt = “I love python programming language”
x = txt.count (“python”)
print (x)

Output:

Python program to demonstrate the use of count() method using optional parameters. Counts the number of times substring occurs in the given string between index 0 and 10 and returns an integer.

वैकल्पिक पैरामीटर का उपयोग करके count() मेथड का उपयोग प्रदर्शित करने के लिए पायथन प्रोग्राम । इंडेक्स () और 10 के बीच दिए गए स्ट्रिंग में घटने वाली संख्या की गणना करता है और एक इंटीजर देता है।

Example 2:
txt = “Love and Live Your Lovable Life”
x = txt.count (“L”)
print (x)
x = txt.count (“L”, 0,10)
print (x)

Output:
4
2

find()

The find() method finds the first occurrence of the specified value. The find() method returns -1 if the value is not found. The find() method is almost the same as the index() method, the only difference is that the index() method raises an exception if the value is not found.

Find() मेथड निर्दिष्ट मूल्य की पहली घटना को ढूंढती है। मान नहीं मिलने पर find) मेथड -1 रिटर्न करती है। find) मेथड लगभग इंडेक्स) मेथड के समान है, केवल अंतर यह है कि इंडेक्स) मेथड एक अपवाद बताती है यदि मूल्य नहीं मिला है।

Syntax: str.find(sub, start, end)

Parameters: पैरामीटर्स

sub: It’s the substring which needs to be searched in the given string.

 सबः यह सबस्ट्रिंग है जिसे दिए गए स्ट्रिंग में सर्च किया जाना है।

start: Starting position where sub is needs to be checked within the string.

स्टार्ट: ऐसी स्थिति शुरू करना जहां सब को स्ट्रिंग के भीतर जांचने की आवश्यकता होती है।

end: Ending position where suffix is needs to be checked within the string.

इंड: इंडिंग पोजीशन जहां सफिक्स को स्ट्रिंग के भीतर जांचने की आवश्यकता होती है।

Note: If start and end indexes are not provided then by default it takes 0 and length-1 as starting and ending indexes where ending indexes is not included in our search.

यदि प्रारंभ और अंत इंडेक्स प्रदान नहीं किए जाते हैं, तो डिफॉल्ट रूप से यह 0 और लंबाई -1 लेता है क्योंकि इंडेक्स शुरू करने और समाप्त करने के लिए इंडेक्स जहां हमारी सर्च में शामिल नहीं है।

Returns: returns the lowest index of the substring if it is found in given string. If it’s not found then it returns -1.

रिटर्नः दिए गए स्ट्रिंग में पाए जाने पर सबसे कम इंडेक्सिंग को रिटर्न करता है। यदि यह नहीं मिला है तो यह -1 देता है।

Example 1: Where in the text is the first occurrence of the letter “e”?
जहां टेक्स्ट में “e” अक्षर की पहली घटना है?
txt = “Hello, Be a light unto yourself.”
x = txt.find(“e”)
print (x)

Output :
1

Example 2: Where in the text is the first occurrence of the letter “i” when you only search between position 3 and 10?
जहां टेक्स्ट में “i” अक्षर की पहली घटना होती है जब आप केवल स्थिति 3 और 10 के बीच खोज करते हैं?
txt = “Be a light unto yourself.”
x = txt.find (“1”,3,10)
print (x)

Output :
-1

Example 3

If the value is not found, the find() method returns -1, but the index() method will raise an exception.

यदि वैल्यू नहीं मिला है, तो find() मेथड -1 पर लौटती है, लेकिन index() मेथड अपवाद को बढ़ाएगी।

txt = “Be a light unto yourself”
print (txt.find(“z”))
print (txt.index (“z”))
Output:
-1
Traceback (most recent call last):
  File “C:\Users\HP\Desktop\1.py”, line 3, in <module>
    print (txt.index (“z”))
ValueError: substring not found

rfind()

The rfind() method finds the last occurrence of the specified value. The rfind() method returns -1 if the value is not found. The rfind() method is almost the same as the rindex() method. rfind()

मेथड निर्दिष्ट वैल्यू की अंतिम घटना को ढूंढती है। यदि मान नहीं मिलता है, तो rfind() मेथड -1 पर वापस आती है। rfind() मेथड लगभग rindex() मेथड के समान है।

Syntax: string.rfind(value, start, end)

Parameter Description

पैरामीटर       डिस्क्रिप्शन

value          Required. The value to search for

 वैल्यू           आवश्यक है।  सर्च के लिए वैल्यू

start           Optional. Where to start the search. Default is 0

स्टार्ट           ऑप्शनल। सर्च कहां से शुरू करें। डिफॉल्ट 0 है।

end            Optional. Where to end the search. Default is. to the end of the string

इंड               ऑप्शनल । सर्च को कहां समाप्त करें। डिफॉल्ट स्ट्रिंग के अंत तक है।

Example 1: Where in the text is the last occurrence of the letter “e”?:

टेक्स्ट में “e” अक्षर की अंतिम घटना कहाँ है?
txt = “Be a light unto yourself”
x = txt.rfind (“e”)
print (x)

Ouput:
21

capitalize()

The capitalize() method returns a string where the first character is upper case.

कैपिटलाईज() मेथड एक स्ट्रिंग पर रिटर्न करती है जहां पहला कैरेक्टर अपर केस है। Syntax: string.capitalize()
Example:
txt = “be a master”
x = txt.capitalize ()
print (x)

Output :
Be a master

title()

The title() method returns a string where the first character in every word is upper case. Like a header, or a title. If the word contains a number or a symbol, the first letter after that will be converted to upper case.

title() मेथड एक स्ट्रिंग देता है जहां हर शब्द में पहला कैरेक्टर अपर केस है। जैसे एक हेडर, या एक टाईटल। यदि शब्द में कोई संख्या या सिम्बल है, तो उसके बाद का पहला अक्षर अपर केस में बदल जाएगा।

Syntax: string.title()

Example:
txt = “welcome 2my world.”
 = txt.title ()
print (x)

Output :
Welcome 2My world

lower()

The lower() method returns a string where all characters are lower case. Symbols and Numbers are ignored.

lower() मेथड एक स्ट्रिंग लौटाती है जहां सभी कैरेक्टर लोवर केस होते हैं। सिम्बल्स और संख्याओं की अनदेखी की जाती है।

Syntax: string.lower

Example:
txt = “ABCDEFGHIJKL”
x = txt.lower ()
print (x)

Output
abcdefghijkl

upper()

The upper() method returns a string where all characters are in upper case. Symbols and Numbers are ignored.

upper() मेथड एक स्ट्रिंग पर रिटर्न होती है जहां सभी कैरेक्टर अपर केस में होते हैं। सिम्बल्स और संख्याओं की अनदेखी की जाती है।

Syntax: string.upper()

Example:
txt = “abcdefghijkl”
x = txt.upper()
print (x)

Output :
ABCDEFGHIJKL

swapcase()

The swapcase() method returns a string where all the upper case letters are lower case and vice versa.

swapcase() मेथड एक स्ट्रिंग रिटर्न है जहां सभी अपर केस लेटर लोवर केस और इसके विपरीत होते हैं।

Syntax: string.swapcase()

Example:
txt = “Hello My Name Is GYANCS”
x = txt. swapcase ()
print (x)

Output :
hELLO mY nAME iS gyancs

islower()

The islower() method returns True if all the characters are in lower case, otherwise False. Numbers, symbols and spaces are not checked, only alphabet characters.

यदि सभी कैरेक्टर लोवर केस में है, तो islower()मेथड टू है, अन्यथा फाल्स। नम्बर्स, सिम्बल और स्पेस की जांच नहीं की जाती है, केवल अल्फाबेट कैरेक्टर।

Syntax: string.islower()

Example:
a = “Hello world!”
b = “hello 123”
c = “be a Light”
print (a.islower())
print (b.islower())
print (c.islower())

Output:
False
True
False

isupper()

The isupper() method returns True if all the characters are in upper case, otherwise False. Numbers, symbols and spaces are not checked, only alphabet characters.

यदि सभी कैरेक्टर अपर केस में हैं, तो isupper() मेथड सही है, अन्यथा गलत। संख्या, सिम्बल और स्पेस की जांच नहीं की जाती है, केवल अल्फाबेट कैरेक्टर की।

Syntax: string.isupper()

Example: 
a = “Hello world!”
b = “hello 123”
c = “BE A LIGHT”
print (a.islower())
print (b.islower())
print (c.islower ( ))

Output:
False
False
True

istitle()

The istitle() method returns True if all words in a text start with a upper case letter, AND the rest of the word are lower case letters, otherwise False. Symbols and numbers are ignored.

istitle() मेथड में यदि किसी टेक्स्ट में सभी शब्द अपर केस लेटर से शुरू होते हैं, और बाकी का शब्द लोअर केस लेटर है, तो टू है, नहीं तो फाल्स। सिम्बल्स और संख्याओं की अनदेखी की जाती है।

Syntax: string.istitle()

Example:
a = “HELLO”
b = “He110”
c = “22 Names”
d = “This Is Pandu’!?”
print(a.istitle())
print (b.istitle())
print (c.istitle())
print (d.istitle())

Output:
False
True
True
True

replace()

The replace() method replaces a specified phrase with another specified phrase.

रिप्लेस() मेथड एक निर्दिष्ट वाक्यांश को दूसरे निर्दिष्ट वाक्यांश के साथ बदल देती है।

Syntax: string.replace(old value, new value, count)

Parameter Description

पैरामीटर       डिस्क्रिप्शन

oldvalue    Required. The string to search for

ओल्डवैल्यू आवश्यक है। सर्च के लिए स्ट्रिंग

newvalue  Required. The string to replace the old value with

न्यूवैल्यू आवश्यक है। पुराने वैल्यू को बदलने के लिए स्ट्रिंग

count         Optional. A number specifying how many occurrences of the old value you want to replace. Default is all occurrences

काउंट ऑप्शनल एक संख्या जो यह निर्दिष्ट करती है कि पुराने मूल्य की कितनी घटनाएं आप बदलना चाहते हैं। डिफॉल्ट सभी घटनाएँ हैं।

Example: Replace the word “disease”:

उदाहरण: वर्ड “disease” को बदलें:
txt = “I like disease”
x = txt.replace (“disease”, “dis ease”)
print (x)

Output :
I like dis ease

strip()

The strip() method removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to remove)

स्ट्रिप) मेथड किसी भी अग्रणी (शुरुआत में स्पेस) और ट्रेलिंग (अंत में स्पेस) कैरेक्टर को हटा देती है (स्पेस को हटाने के | लिए डिफॉल्ट लिडिंग कैरेक्टर है)

Syntax: string.strip(characters)

Example:
txt = ”   mango   “
x = txt.strip()
print (x, ” is my favorite”)
txt = “,,,rrrttgg…..mango….rrr”
x = txt.strip (“,.grt”)
print (x)

Output:
mango is my favorite
mango

istrip()

 The Istrip() method remove spaces to the left of the string (space is the default leading character to remove).

istrip() मेथड स्ट्रिंग के बाईं ओर रिक्त स्थान को हटाती है (स्थान को हटाने के लिए डिफॉल्ट प्रमुख कैरेक्टर है)

Syntax: string.Istrip(characters)

Example:
txt = “INDIA “
x = txt.lstrip()
print(” for all countries”, x, ” is my favorite country”)

Output:
for all countries INDIA  is my favorite country

rstrip()

The rstrip() method remove spaces to the right of the string (space is the default leading character to remove.

rstrip() मेथड स्ट्रिंग के दाई ओर स्पेस को हटाती है (स्पेस को हटाने के लिए डिफॉल्ट लिडिंग कैरेक्टर है)

Syntax: string.rstrip(characters)

Example:
txt=”           INDIA           “
x = txt.rstrip ()
print(” for all countries”, x, ” is my favorite country”)

Output:
for all countries INDIA is my favorite country

partition ()

The partition() method searches for a specified string, and splits the string into a tuple containing three elements. The first element contains the part before the specified string. The second element contains the specified string. The third element contains the part after the string.

पार्टीशन) मेथड एक निर्दिष्ट स्ट्रिंग को सर्च करती है, और स्ट्रिंग को तीन तत्वों से युक्त ट्यूपल में विभाजित करती है। पहले एलीमेंट में निर्दिष्ट स्ट्रिंग से पहले का हिस्सा है। दूसरे एलीमेंट में निर्दिष्ट स्ट्रिंग शामिल है। तीसरे एलीमेंट में स्ट्रिंग के बाद का हिस्सा होता है।

Note: This method search for the first occurrence of the specified string.

नोट: यह मेथड निर्दिष्ट स्ट्रिंग की पहली घटना के लिए सर्च करती है।

Syntax: string.partition(value)

Example 1:
txt = “be a master”
x = txt.partition (“a”)
print (x)

Output:
(‘be’, ‘a’, ‘master’)

Example 2:

txt = “be a master”
x = txt.partition (“z”)
print (x)

Output:
(‘be a master’, ”, ”)

join()

The join() method takes all items in an iterable and joins them into one string. A string must be specified as the separator.

ज्वाइन () मेथड सभी वस्तुओं को एक पुनरावृत्त में ले जाती है और उन्हें एक स्ट्रिंग में जोड़ देती है। एक स्ट्रिंग को सेपरेटर के रूप में निर्दिष्ट किया जाना चाहिए।

Syntax: string.join(iterable)

Example:
myTuple = (“aaa”, “bbb”, “ccc”)
x = “@ “.join (myTuple)
print (x)

Output :
aaa @ bbb @ ccc

Note: When using a dictionary as an iterable, the returned values are the keys, not the values.

नोट: डिक्शनरी को पुनरावृति के रूप में उपयोग करते समय, रिटर्न हुए वैल्यू कीज हैं, न कि वैल्यू ।

Join all items in a dictionary into a string, using a word “###” as separator:

सेपरेटर के रूप में “###” शब्द का उपयोग करते हुए एक स्ट्रिंग में एक डिक्शनरी में सभी आइटम को मिलाएं।

Example:
myDict = { “name”: “Gyancs”, “country”: “India”}
mySeparator = “###”
x = mySeparator.join (myDict)
print (x)

Output
name###country

isspace()

The isspace() method returns True if all the characters in a string are whitespaces, otherwise False.

isspace() मेथड टू पर रिटर्न होता है यदि स्ट्रिंग के सभी कैरेक्टर व्हाइटस्पेस हैं, अन्यथा फाल्स है।

Syntax: string.isspace()

Example:

txt = ”  “
x = txt.isspace ()
print (x)
txt = ” S “
x = txt.isspace ()
print (x)

Output:
True
False

isalpha()

The isalpha() method returns True if all the characters are alphabet letters (a-z). Example of characters that are not alphabet letters: (space)!#%&? etc.

यदि सभी करैक्टर लेटर (a-z) है तो isalpha() मेथड टू है । करैक्टर का उदाहरण जो अल्फाबेट नहीं है (स्पेस)!#%&? आदि ।  

Syntax: string.isalpha()

Example:
txt = “Gyancs School”
x = txt.isalpha ()
print (x)
txt = “Gyancs School pvt ltd.,”
x = txt.isalpha ()
print (x)

Output:
True
False

isdigit()

The isdigit() method returns True if all the characters are digits, otherwise False. Exponents, like? are also considered to be a digit.

यदि सभी कैरेक्टर अंक हैं, तो isdigit() मेथड टू है, अन्यथा फाल्स। एक्सपोनेन्ट जैसे को भी एक अंक माना जाता है।

Syntax: string.isdigit()

Example:
a = “\u0030” #unicode for 0
b = “\u00B2” #unicode for 2
print (a.isdigit ())
print (b.isdigit())
txt = “50800”
x = txt.isdigit()
print (x)
txt = “A”
x = txt.isdigit()
print (x)

Output:
True
True
True
False

isalnum()

 The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9). Example of characters that are not alphanumeric: (space)!#%&? etc.,

यदि सभी कैरेक्टर अल्फान्यूमेरिक हैं, जिसका अर्थ अल्फाबेट लेटर (a-z) और संख्याएँ (0-9) हैं, तो isalnum() मेथड सही है। कैरेक्टर का उदाहरण जो अल्फान्यूमेरिक नहीं हैं: (स्पेस)!#%&? आदि।

Syntax: string.isalnum()

Example:
txt = “Nov20”
x = txt.isalnum ()
print (x)
txt = “Nov 20”
x = txt.isalnum ()
print (x)

Output:
True
False

startswith()

The startswith() method returns True if the string starts with the specified value, otherwise False.

स्टार्टविथ() मेथड टू है यदि स्ट्रिंग निर्दिष्ट वैल्यू से शुरू होती है, अन्यथा फाल्स है।

Syntax: string.startswith(value, start, end)

Parameter   Description

पैरामीटर        डिस्क्रिप्शन

value   Required. The value to check if the string starts with

वैल्यू    आवश्यक है। यदि स्ट्रिंग से शुरू होता है, तो यह जांचने का वैल्यू

start   Optional. An Integer specifying at which position to start the search

स्टार्ट    वैकल्पिक  सर्च शुरू करने के लिए किस स्थिति में एक इंटीजर निर्दिष्ट करता है।

end   Optional. An Integer specifying at which position to end the search

इंड   वैकल्पिक । सर्च को समाप्त करने के लिए किस स्थिति में एक इंटीजर निर्दिष्ट करता है।

Example:
txt = “Hello, welcome to my world.”
x = txt.startswith (“Hello”)
print (x)
x = txt.startswith (“wei”, 7, 20)
print (x)

Output:
True
True

endswith()

The endswith() method returns True if the string ends with the specified value, otherwise False. इंड्सविथ() मेथड टू है यदि स्ट्रिंग निर्दिष्ट वैल्यू के साथ समाप्त होती है, अन्यथा फाल्स।

Syntax: string.startswith(value, start, end)

Parameter Description

पैरामीटर       डिस्क्रिप्शन

value          Required. The value to check if the string starts with

वैल्यू            आवश्यक है। यदि स्ट्रिंग से शुरू होता है, तो यह जांचने का वैल्यू

start          Optional. An Integer specifying at which position to start the search

स्टार्ट           वैकल्पिक सर्च शुरू करने के लिए किस स्थिति में एक इंटीजर निर्दिष्ट करता है

end            Optional. An Integer specifying at which position to end the search

इंड               वैकल्पिक  सर्च को समाप्त करने के लिए किस स्थिति में एक इंटीजर निर्दिष्ट करता है

Example:
txt = “Hello, welcome to my world.”
x = txt.endswith (“world.”)
print (x)
x = txt.startswith(“to”, 7, 20)
print (x)

Output:
True
False

encode ()

The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.

ईनकोड) मेथड निर्दिष्ट एन्कोडिंग का उपयोग करके स्ट्रिंग को एन्कोड करता है। यदि कोई एन्कोडिंग निर्दिष्ट नहीं है, तो UTF-8 का उपयोग किया जाएगा।

Syntax: string.encode(encoding=encoding, errors=errors)

Parameter    Description

पैरामीटर           डिस्क्रिप्शन

encoding      Optional. A String specifying the encoding to use. Default is UTF-8

ईनकोडिंग        वैकल्पिक । एक स्ट्रिंग उपयोग करने के लिए एन्कोडिंग को निर्दिष्ट करता है।  डिफॉल्ट UTF-8  है

errors      Optional. A String specifying the error method. Legal values are:

एरर           वैकल्पिक । एक स्ट्रिंग एरर मेथड को निर्दिष्ट करता है। लीगल वैल्यू हैं:

Example:
txt = “Style”
print (txt.encode (encoding=”ascii”, errors=”backslashreplace”) )
print (txt.encode (encoding=”ascii”, errors=”ignore”))
print (txt.encode (encoding=”ascii”, errors=”namereplace”))
print (txt.encode (encoding=”ascii”, errors=”replace”))
print (txt.encode (encoding=”ascii”, errors=”xmlcharrefreplace”))

Output:
b’Style’
b’Style’
b’Style’
b’Style’
b’Style’

decode()

This method is used to convert from one encoding scheme, in which argument string is encoded to the desired encoding scheme. This works opposite to encode. It accepts the encoding of the encoding string to decode it and returns the original string.

इस पद्धति का उपयोग एक एन्कोडिंग स्कीम से परिवर्तित करने के लिए किया जाता है, जिसमें तर्क स्ट्रिंग वांछित एन्कोडिंग योजना के लिए एन्कोडेड है। यह एन्कोड करने के विपरीत काम करता है। यह इसे डिकोड करने के लिए एन्कोडिंग स्ट्रिंग के एन्कोडिंग को स्वीकार करता है और मूल स्ट्रिंग को रिटर्न करता है।

Syntax: decode(encoding, error)

encoding : Specifies the encoding on the basis of which decoding has to be performed.

ईनकोडिंग  : एन्कोडिंग निर्दिष्ट करता है जिसके आधार पर डिकोडिंग का परफॉर्म किया जाना है।

error        : Decides how to handle the errors if they occur, example ‘strict’ raises Unicode error in case of exception and ‘ignore’ ignores the errors occurred.

एरर           : यदि वे होते हैं तो एरर को कैसे संभालना है, यह तय करता है, उदाहरण ‘strict’ अपवाद के मामले में  

                 यूनिकोड एरर रेज करता है और एरर को अनदेखा करने पर अपवाद अनदेखा करता है।

Membership – मेम्बरशिप

We can use membership operators to check whether the given object present in the given collection (it may be string, set, list, tuple or dict).

हम मेम्बरशिप ऑपरेटर्स का उपयोग यह जांचने के लिए कर सकते हैं कि दिए गए संग्रह में मौजूद ऑब्जेक्ट (यह स्ट्रिंग, सेट, लिस्ट, ट्यूपल या dict हो सकती है)

1. in: The ‘in’operator is used to check if a value exists in a sequence or not. Evaluates to true

if it finds a variable in the specified sequence and false otherwise.

in’ ऑपरेटर का उपयोग यह जांचने के लिए किया जाता है कि वैल्यू किसी अनुक्रम में मौजूद है या नहीं। टू का

मूल्यांकन करता है अगर यह निर्दिष्ट अनुक्रम में एक वैरियेबल पाता है और अन्यथा फाल्स है।

2. not in: Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.

टू का मूल्यांकन करता है यदि यह निर्दिष्ट अनुक्रम में एक वैरियेबल नहीं पाता है और अन्यथा फाल्स है।

Example:
str = “you create your own reality”
list1 = [“rama”, “jhony”, “parvin”]
print(“you” in str)
print(“You” in str)
print(“You” not in str)
print(“you” not in str)
print(‘rama’ in list1)
print(‘sita’ in list1)
print(‘sita’ not in list1)
print(‘rama’ not in list1)

Output:
True
False
True
False
True
False
True
False

Chapter wise Model Paper Link

About Me