Skip to content

Commit 6190d94

Browse files
committed
updated string methods
1 parent 5e8eedd commit 6190d94

1 file changed

Lines changed: 309 additions & 0 deletions

File tree

Python Tutorial/Strings.md

Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,312 @@ print(S)
245245
|index() |Python String index() function returns the lowest index where the specified substring is found. |
246246
| __contains__()|Python String class has __contains__() function that we can use to check if it contains another string or not. We can also use | | | “in” operator to perform this check. |
247247

248+
249+
##### Built-in String Functions in Python
250+
251+
`Conversion Functions`
252+
253+
1. capitalize() – Returns the string with the first character capitalized and rest of the characters in lower case.
254+
```python
255+
var = 'PYTHON'
256+
print (var.capitalize())
257+
# Python
258+
```
259+
2. lower() – Converts all the characters of the String to lowercase
260+
```python
261+
var = 'FootBall'
262+
print (var.lower())
263+
# football
264+
```l
265+
3. upper() – Converts all the characters of the String to uppercase
266+
```python
267+
var = 'FootBall'
268+
print (var.upper())
269+
# FOOTBALL
270+
```
271+
4. swapcase() – Swaps the case of every character in the String means that lowercase characters got converted to uppercase and vice-versa.
272+
```python
273+
var = 'FootBall'
274+
print (var.swapcase())
275+
# fOOTbALL
276+
```
277+
5. title() – Returns the ‘titlecased’ version of String, which means that all words start with uppercase and the rest of the characters in words are in lowercase.
278+
```python
279+
var = 'welcome to Python programming'
280+
print (var.title())
281+
# Welcome To Python Programming
282+
```
283+
6. `count( str[, beg [, end]])` – Returns the number of times substring ‘str’ occurs in the range [beg, end] if beg and end index are given else the search continues in full String Search is case-sensitive.
284+
```python
285+
var='Television'
286+
str='e'
287+
print (var.count(str))
288+
# 2
289+
var1='Eagle Eyes'
290+
print (var1.count('e'))
291+
# 2
292+
var2='Eagle Eyes'
293+
print (var2.count('E',0,5))
294+
# 1
295+
```
296+
`Comparison Functions – Part1`
297+
1. islower() – Returns ‘True’ if all the characters in the String are in lowercase. If any of the char is in uppercase, it will return False.
298+
```python
299+
var='Python'
300+
print (var.islower())
301+
# False
302+
303+
var='python'
304+
print (var.islower())
305+
# True
306+
```
307+
2. isupper() – Returns ‘True’ if all the characters in the String are in uppercase. If any of the char is in lowercase, it will return False.
308+
```python
309+
var='Python'
310+
print (var.isupper())
311+
# False
312+
313+
var='PYTHON'
314+
print (var.isupper())
315+
# True
316+
```
317+
3. isdecimal() – Returns ‘True’ if all the characters in String are decimal. If any character in the String is of other data-type, it will return False.Decimal characters are those from the Unicode category Nd.
318+
```python
319+
num=u'2016'
320+
print (num.isdecimal())
321+
# True
322+
```
323+
4. isdigit() – Returns ‘True’ for any char for which isdecimal() would return ‘True and some characters in the ‘No’ category. If there are any characters other than these, it will return False’.
324+
325+
Precisely, digits are the characters for which Unicode property includes: Numeric_Type=Digit or Numeric_Type=Decimal.
326+
327+
For example, superscripts are digits, but fractions not.
328+
```python
329+
print ('2'.isdigit())
330+
# True
331+
332+
print ('²'.isdigit())
333+
# True
334+
```
335+
`Comparison Functions – II`
336+
1. isnumeric() – Returns ‘True’ if all the characters of the Unicode String lie in any one of the categories Nd, No, and NI.
337+
338+
If there are any characters other than these, it will return False.
339+
340+
Precisely, Numeric characters are those for which Unicode property includes: Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.
341+
```python
342+
num=u'2016'
343+
print (num.isnumeric())
344+
# True
345+
346+
num=u'year2016'
347+
print (num.isnumeric())
348+
# False
349+
```
350+
2. isalpha() – Returns ‘True’ if String contains at least one character (non-empty String), and all the characters are alphabetic, ‘False’ otherwise.
351+
```python
352+
print ('python'.isalpha())
353+
# True
354+
355+
print ('python3'.isalpha())
356+
# False
357+
```
358+
3. isalnum() – Returns ‘True’ if String contains at least one character (non-empty String), and all the characters are either alphabetic or decimal digits, ‘False’ otherwise.
359+
```python
360+
print ('python'.isalnum())
361+
# True
362+
print ('python3'.isalnum())
363+
# True
364+
```
365+
`Padding Functions`
366+
1. rjust(width[,fillchar]) – Returns string filled with input char while pushing the original content on the right side.
367+
368+
By default, the padding uses a space. Otherwise, ‘fillchar’ specifies the filler character.
369+
```python
370+
var='Python'
371+
print (var.rjust(10))
372+
# Python
373+
374+
print (var.rjust(10,'-'))
375+
# ----Python
376+
```
377+
2. ljust(width[,fillchar]) – Returns a padded version of String with the original String left-justified to a total of width columns
378+
379+
By default, the padding uses a space. Otherwise, ‘fillchar’ specifies the filler character.
380+
```python
381+
var='Python'
382+
print (var.ljust(10))
383+
# Python
384+
385+
print (var.ljust(10,'-'))
386+
# Python----
387+
```
388+
3. center(width[,fillchar]) – Returns string filled with the input char while pushing the original content into the center.
389+
390+
By default, the padding uses a space. Otherwise, ‘fillchar’ specifies the filler character.
391+
```python
392+
var='Python'
393+
print (var.center(20))
394+
# Python
395+
396+
print (var.center(20,'*'))
397+
# *******Python*******
398+
```
399+
4. zfill(width) – Returns string filled with the original content padded on the left with zeros so that the total length of String becomes equal to the input size.
400+
401+
If there is a leading sign (+/-) present in the String, then with this function, padding starts after the symbol, not before it.
402+
```python
403+
var='Python'
404+
print (var.zfill(10))
405+
# 0000Python
406+
407+
var='+Python'
408+
print (var.zfill(10))
409+
# +000Python
410+
```
411+
`Search Functions`
412+
1. find(str [,i [,j]]) – Searches for ‘str’ in complete String (if i and j not defined) or in a sub-string of String (if i and j are defined).This function returns the index if ‘str’ is found else returns ‘-1’.
413+
414+
Here, i=search starts from this index, j=search ends at this index.
415+
See more details – Python String Find()
416+
```python
417+
var="High sky"
418+
str="sky"
419+
print (var.find(str))
420+
# 5
421+
422+
var="High sky"
423+
str="sky"
424+
print (var.find(str,4))
425+
# 5
426+
427+
var="High sky"
428+
str="sky"
429+
print (var.find(str,7))
430+
# -1
431+
```
432+
2. index(str[,i [,j]]) – This is same as ‘find’ method. The only difference is that it raises the ‘ValueError’ exception if ‘str’ doesn’t exist.
433+
```python
434+
var='High Sky'
435+
str='sky'
436+
print (var.index(str))
437+
# 5
438+
439+
var='High Sky'
440+
str='sky'
441+
print (var.index(str,4))
442+
# 5
443+
444+
var='High Sky'
445+
str='sky'
446+
print (var.index(str,7))
447+
# ValueError: substring not found
448+
```
449+
3. rfind(str[,i [,j]]) – This is same as find() just that this function returns the last index where ‘str’ is found. If ‘str’ is not found, it returns ‘-1’.
450+
```python
451+
var='This is a good example'
452+
str='is'
453+
print (var.rfind(str,0,10))
454+
# 5
455+
456+
print (var.rfind(str,10))
457+
# -1
458+
```
459+
4. count(str[,i [,j]]) – Returns the number of occurrences of substring ‘str’ in the String. Searches for ‘str’ in the complete String (if i and j not defined) or in a sub-string of String (if i and j are defined).
460+
461+
Where: i=search starts from this index, j=search ends at this index.
462+
```python
463+
var='This is a good example'
464+
str='is'
465+
print (var.count(str))
466+
# 2
467+
468+
print (var.count(str,4,10))
469+
# 1
470+
```
471+
472+
`String Substitution Functions`
473+
1. replace(old,new[,count]) – Replaces all the occurrences of substring ‘old’ with ‘new’ in the String.
474+
475+
If the count is available, then only ‘count’ number of occurrences of ‘old’ will be replaced with the ‘new’ var.
476+
477+
Where old =substring to replace, new =substring
478+
```python
479+
var='This is a good example'
480+
str='was'
481+
print (var.replace('is',str))
482+
# Thwas was a good exampleprint (var.replace('is',str,1))
483+
# Thwas is a good example
484+
```
485+
2. split([sep[,maxsplit]]) – Returns a list of substring obtained after splitting the String with ‘sep’ as a delimiter.
486+
487+
Where, sep= delimiter, the default is space, maxsplit= number of splits to be done
488+
```python
489+
var = "This is a good example"
490+
print (var.split())
491+
# ['This', 'is', 'a', 'good', 'example']print (var.split(' ', 3))
492+
# ['This', 'is', 'a', 'good example']
493+
```
494+
3. splitlines(num) – Splits the String at line breaks and returns the list after removing the line breaks.
495+
496+
Where num = if this is a positive value. It indicates that line breaks will appear in the returned list.
497+
```python
498+
var='Print new line\nNextline\n\nMove again to new line'
499+
print (var.splitlines())
500+
# ['Print new line', 'Nextline', '', 'Move again to new line']print (var.splitlines(1))
501+
# ['Print new line\n', 'Nextline\n', '\n', 'Move again to new line']
502+
```
503+
4. join(seq) – Returns a String obtained after concatenating the sequence ‘seq’ with a delimiter string.
504+
505+
Where: the seq= sequence of elements to join
506+
```python
507+
seq=('ab','bc','cd')
508+
str='='
509+
print (str.join(seq))
510+
# ab=bc=cd
511+
```
512+
513+
`Misc String Functions`
514+
1. lstrip([chars]) – Returns a string after removing the characters from the beginning of the String.
515+
516+
Where: Chars=this is the character to be trimmed from the String.
517+
518+
The default is whitespace character.
519+
```python
520+
var=' This is a good example '
521+
print (var.lstrip())
522+
# This is a good example
523+
var='*****This is a good example*****'
524+
print (var.lstrip('*'))
525+
# This is a good example**********
526+
```
527+
2. rstrip() – Returns a string after removing the characters from the End of the String.
528+
529+
Where: Chars=this is the character to be trimmed from the String. The default is whitespace character.
530+
```python
531+
var=' This is a good example '
532+
print (var.rstrip())
533+
# This is a good example
534+
var='*****This is a good example*****'
535+
print (var.lstrip('*'))
536+
# *****This is a good example
537+
```
538+
3. rindex(str[,i [,j]]) – Searches for ‘str’ in the complete String (if i and j not defined) or in a sub-string of String (if i and j are defined). This function returns the last index where ‘str’ is available.
539+
540+
If ‘str’ is not there, then it raises a ValueError exception.
541+
542+
Where: i=search starts from this index, j=search ends at this index.
543+
```python
544+
var='This is a good example'
545+
str='is'
546+
print (var.rindex(str,0,10))
547+
# 5
548+
print (var.rindex(str,10))
549+
# ValueError: substring not found
550+
```
551+
4. len(string) – Returns the length of given String
552+
```python
553+
var='This is a good example'
554+
print (len(var))
555+
# 22
556+
```

0 commit comments

Comments
 (0)