Skip to content

Commit 1a60def

Browse files
authored
Update StringMethods.md
1 parent a6d1dca commit 1a60def

1 file changed

Lines changed: 57 additions & 33 deletions

File tree

‎Data_Structure_In_Python/String/StringMethods.md‎

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,51 @@
66
Following 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

1111
Example:
1212
```python
1313
text="hello"
1414
result=txt.capitalize()
1515
print(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+
2123
Example:
2224
```python
2325
text1="Hello"
2426
result=text1.count('l')
2527
result1=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
```
3136
Syntax:
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+
3844
For Example
3945
```python
4046
text1="This is simple Text"
4147
result=text1.count('i')
4248
res=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:
5261
text1="This is simple Text."
5362
result=text1.endswith('.')
5463
print(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

6271
Example:
6372
```python
6473
mystring="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
7689
Syntax
7790
```python
7891
string.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+
8397
Example :one::
8498
```python
8599
text="Python"
86100
result=text.find("t")
87101
print(result)
88-
#result:2
102+
#Result:2
89103
```
90104
Example :two:
91105
```python
92106
mystring="i love to play cricket and football"
93107
print(mystring.find("o"))
94-
#result:3
108+
#Result:3
95109
```
96110
Example :three: :
97111
```python
98112
mystring="i love to play cricket and football"
99113
print(mystring.find("cric"))
100-
#result:15
114+
#Result:15
101115
```
102116
When 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
107121
mystring="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
114130
mystring="I am python Expert"
115131
search=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
131151
mystring="india pakistan china america"
132152
#split a string with whitespace
133153
splitstring=mystring.split(' ') #or mystring.split()
134154
print(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"
141162
a,b,c,d=mystring.split(' ')
142163
print(a)
143164
print(b)
144165
print(c)
145166
print(d)
146167

147168
#Result :
169+
'''
148170
india
149171
pakistan
150172
china
151173
america
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

158181
Syntax:
159182
```python
160183
Sepeartor.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

164187
Example:
165188
```python
166189
mylist=['india', 'pakistan', 'china', 'america']
167190
strings=','.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'
181205
newstring=stat.replace('is','was')
182206
print(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'
187211
Example:

0 commit comments

Comments
 (0)