66Following are the most common used method for strings are
77
88## 1. capitalize() :a : :
9- - The ** capitalize()** methodupper case the first letter in the given sequences.
9+ - The ** capitalize()** method upper case the first letter in the given sequences(string) .
1010
1111Example:
1212``` python
1313text= " hello"
1414result= txt.capitalize()
1515print (result)
16- # result :Hello
16+ # Result :Hello
1717```
1818
1919## 2. count() :seven : :
2020- The ** count()** method returns the number of times a specified value appears in the string.
21+ - Returns the frequency of given element inside string.
22+
2123Example:
2224``` python
2325text1= " Hello"
2426result= text1.count(' l' )
2527result1= text1.count(' H' )
2628
27- print (result) # result:2
28- print (result1) # result:1
29+ print (result)
30+ # Result:2
31+
32+ print (result1)
33+ # Result:1
2934
3035```
3136Syntax:
3237``` python
33- string.count(value, start, end)
38+ string.count(value, start, end)
3439```
35- 1 . value:- Required. A String. The string to value to search for
36- 2 . start:- Optional. An Integer. The position to start the search. Default is 0
37- 3 . end:- Optional. An Integer. The position to end the search. Default is the end of the string
40+ 1 . value:- element which do you want to find count.
41+ 2 . start:- Default is 0,starting position of an element.
42+ 3 . end:- Defualt is len(string) ,ending position of an element.
43+
3844For Example
3945``` python
4046text1= " This is simple Text"
4147result= text1.count(' i' )
4248res= text1.count(' i' ,6 ,len (text1))
43- print (result) # result:3
44- print (res) # result :1
49+ print (result)
50+ # Result:3
51+
52+ print (res)
53+ # Result :1
4554```
4655## 3. endswith() :speech_balloon : :
4756
@@ -52,18 +61,22 @@ Example:
5261text1= " This is simple Text."
5362result= text1.endswith(' .' )
5463print (result)
55- # result :True
64+ # Result :True
5665````
5766
5867# # 4. index() :
59- - The ** index()** method finds the first occurrence of the specified value.
60- - The ** index()** method raises an exception if the value is not found.
68+ - The ** index()** method finds the first occurrence of the specified value.
69+ - The ** index()** method raises an exception if the value is not found.
6170
6271Example:
6372```python
6473mystring= " I am python"
65- print (mystring.index(' a' )) # result:2
66- print (mystring.index(' k' )) # result:error
74+ print (mystring.index(' a' ))
75+ # Result:2
76+
77+ print (mystring.index(' k' ))
78+ # Result:error:-substring not found
79+
6780```
6881
6982
@@ -76,40 +89,43 @@ print(mystring.index('k')) #result:error
7689Syntax
7790``` python
7891string.find(str , startpoint, endpoint)
79- # str:-which string to find
80- # startpoint:-the starting index from where you want to find string
81- # endpoint:-Ending index
8292```
93+ ** str** :-which string to find
94+ ** startpoint** :-the starting index from where you want to find string
95+ ** endpoint** :-Ending index
96+
8397Example :one : :
8498``` python
8599text= " Python"
86100result= text.find(" t" )
87101print (result)
88- # result :2
102+ # Result :2
89103```
90104Example :two :
91105``` python
92106mystring= " i love to play cricket and football"
93107print (mystring.find(" o" ))
94- # result :3
108+ # Result :3
95109```
96110Example :three : :
97111``` python
98112mystring= " i love to play cricket and football"
99113print (mystring.find(" cric" ))
100- # result :15
114+ # Result :15
101115```
102116When you give multiple character string it will find out the starting point of string where it founds.
103117
104118
105119- if we want to find out second occurence of of required string
106120``` python
107121mystring= " i love to play cricket and football"
108- first= (mystring.find(" o" )) second= (mystring.find(" o" ,first+ 1 )) print (second)
109- # result:8
122+ first= (mystring.find(" o" ))
123+ second= (mystring.find(" o" ,first+ 1 ))
124+ print (second)
125+ # Result:8
110126```
111127
112- - If searching string not present in input string
128+ - If searching string not present in input string it return -1.
113129``` python
114130mystring= " I am python Expert"
115131search= mystring.find(" this" )
@@ -118,54 +134,62 @@ print(search)
118134```
119135### To find all occurrence of word :beetle :
120136``` python
121- for i,j in enumerate (mystring):
122- if j== ' o' :
123- print (i)
137+ mystring= " I am python Expert"
138+ for i,j in enumerate (mystring):
139+ if j== ' t' :
140+ print (i)
141+ # Result:7 17
124142```
125143
126144
127145
128146## 6. Split() :frog : :
129147- if you want to separate a string with white space or commas or some special character then split method is used
148+ - ** split()** method returns the list object.
149+
130150``` python
131151mystring= " india pakistan china america"
132152# split a string with whitespace
133153splitstring= mystring.split(' ' ) # or mystring.split()
134154print (splitstring)
135155
136- # result : ['india', 'pakistan', 'china', 'america']
156+ # Result : ['india', 'pakistan', 'china', 'america']
137157```
138158
139- - if you want to split that string and store it in different variable
159+ - if you want to split a string and store its values in different variable.
140160``` python
161+ mystring= " india pakistan china america"
141162a,b,c,d= mystring.split(' ' )
142163print (a)
143164print (b)
144165print (c)
145166print (d)
146167
147168# Result :
169+ '''
148170india
149171pakistan
150172china
151173america
174+ '''
152175```
153176
154177## 7. join() :arrows_clockwise : :
155- - The join() method takes all items in an iterable and joins them into one string.
178+ - The ** join()** method takes all items in an iterable and joins them into one string.
156179- A string must be specified as the separator.
157180
158181Syntax:
159182``` python
160183Sepeartor.join(iterable objects)
161184```
162- ** Sepeartor** : any thing that you want to use as sepeartor such as -,, ', - , whitespaces, numbers alphabets, special characters
185+ ** Sepeartor** : any thing that you want to use as sepeartor such as '-',',',''','-' , whitespaces, numbers alphabets, special characters
163186
164187Example:
165188``` python
166189mylist= [' india' , ' pakistan' , ' china' , ' america' ]
167190strings= ' ,' .join(mylist)
168- print (strings) # result:india,pakistan,china,america
191+ print (strings)
192+ # Result:india,pakistan,china,america
169193```
170194
171195## 8. replace() :clipboard : :
@@ -181,7 +205,7 @@ stat='kat is good girl'
181205newstring= stat.replace(' is' ,' was' )
182206print (newstring)
183207
184- # result : kat was good girl
208+ # Result : kat was good girl
185209```
186210- Replace the 'n' first occurrence of the word 'is'
187211Example:
0 commit comments