Some Functions of String


☞Following are the functions of string :

1. len()
2. count()
3. index()
4. capitalize()
5. isalnum()
6. isalpha()
7. isdigit()
8. isspace()
9. lower()
10. islower()
11. upper()
12. isupper()
13. replace()
14. split()
15. partition(separator)
16. istitle()
17. title()
18. join(sequence)
19. swapcase()
20. find()
21. strip()
22. lstrip()
23. rstrip()
24. startswith()
25. endswith()
26. ord()
27. chr()

len() :

  • It returns the length of the string.
Syntax :
 
len(<string>)
Example :
 
S1="inspirewebsoft"
print(len(S1))

S2="Hello\n\nWorld"
print(len(S2))

S3=""
print(len(S3))   
Output :
 
14
12
0   

count() :

  • It is used to count the occurrences of a character/string in the string.
Syntax :
 
<string>.count(<character/string>)    
Example :
 
S1="inspirewebsoft"
print(S1.count('e'))

S2="HelloHelloWorldWorldHel"
print(S2.count("Hello"))

S3="inspirewebsoft"
print(S2.count("a"))    
Output :
 
2
2
0    

index() :

  • It finds the first index of a given character and returns the index from the string.
Syntax :
 
<string>.index( <character>)
<string>.index( <character>, <start_index>)
<string>.index( <character>, <start_index>, <stop_index>)
Example 1 :
 
S1="inspirewebsoft"
print(S1.index('i'))

S2="Hello Worlo"
print(S2.index('lo'))

S3="inspirewebsoft"
print(S3.index('i',2))

S4="inspirewebsoft"
print(S4.index('i',2,9))

S4="inspirewebsoft"
print(S4.index('i',2,20))  
Output :
 
0
3
4
4
4   
Example 2 :
 
S1="inspirewebsoft"
print(S1.index('a'))  
Output :
 
print(S1.index('a'))
ValueError: substring not found   

capitalize() :

  • It is used to capitalize the first letter of the string and all the other letters will be converted to small letters.
  • If string consist of integers or any other special symbol, then these characters will remain unchanged.
Syntax :
 
<string>.capitalize()
Example :
 
S1="inspireWEBsoft"
print(S1.capitalize())

S2="inspire WEB soft"
print(S2.capitalize())

S3="4inspireWEBsoft"
print(S3.capitalize())

S4="  "
print(S4.capitalize())

S5="123"
print(S5.capitalize())  
Output :
 
Inspirewebsoft
Inspire web soft
4inspirewebsoft
    
123   

isalnum() :

  • It returns True if the characters in the string are alphanumeric (alphabets or numbers) and there is at least one character, False otherwise.
Syntax :
 
<string>.isalnum()
Example :
 
S1="inspireWEBsoft"
print(S1.isalnum())

S2="inspireWEBsoft123"
print(S2.isalnum())

S3="inspireWEBsoft  123"
print(S3.isalnum())

S4="123"
print(S4.isalnum())

S5="123.56"
print(S5.isalnum())

S6="@"
print(S6.isalnum())  
Output :
 
True
True
False
True
False
False   

isalpha() :

  • It returns True if the string contains only letters, otherwise returns False.
Syntax :
 
<string>.isalpha()
Example :
 
S1="inspireWEBsoft"
print(S1.isalpha())

S2="inspire WEB soft"
print(S2.isalpha())

S3="inspireWEBsoft123"
print(S3.isalpha())  
Output :
 
True
False
False   

isdigit() :

  • It returns True if the string contains only digits, otherwise False.
Syntax :
 
<string>.isdigit()
Example :
 
S1="inspireWEBsoft"
print(S1.isdigit())

S2="inspire WEB soft"
print(S2.isdigit())

S3="inspireWEBsoft123"
print(S3.isdigit())

S4="123"
print(S4.isdigit())

S5="123.45"
print(S5.isdigit())
Output :
 
False
False
False
True
False

isspace() :

  • It returns True if the string contains only whitespace characters, otherwise returns False.
Syntax :
 
<string>.isspace()
Example :
 
S1="inspireWEBsoft"
print(S1.isspace())

S2="inspire WEB soft"
print(S2.isspace())

S3=" "              #1 space
print(S3.isspace())

S4="     "           #5 spaces
print(S4.isspace())
Output :
 
False
False
True
True

lower() :

  • It returns a copy of the string converted to lowercase.
Syntax :
 
<string>.lower()
Example :
 
S1="inspireWEBsoft"
print(S1.lower())

S2="inspire WEB soft"
print(S2.lower())

S3="123"        
print(S3.lower())

S4="inspireWEBsoft@123"
print(S4.lower())
Output :
 
inspirewebsoft
inspire web soft
123
inspirewebsoft@123

islower() :

  • It returns True if the string is in lowercase.
Syntax :
 
<string>.islower()
Example :
 
S1="inspireWEBsoft"
print(S1.islower())

S2="inspire WEB soft"
print(S2.islower())

S3="123"        
print(S3.islower())

S4="inspireWEBsoft@123"
print(S4.islower())

S5="inspirewebsoft"
print(S5.islower())
Output :
 
False
False
False
False
True

upper() :

  • It returns a copy of the string converted to uppercase.
Syntax :
 
<string>.upper()
Example :
 
S1="inspireWEBsoft"
print(S1.upper())

S2="inspire WEB soft"
print(S2.upper())

S3="123"        
print(S3.upper())

S4="inspireWEBsoft@123"
print(S4.upper())
Output :
 
INSPIREWEBSOFT
INSPIRE WEB SOFT
123
INSPIREWEBSOFT@123

isupper() :

  • It returns True if the string is in uppercase.
Syntax :
 
<string>.isupper()
Example :
 
S1="inspireWEBsoft"
print(S1.isupper())

S2="inspire WEB soft"
print(S2.isupper())

S3="123"        
print(S3.isupper())

S4="inspireWEBsoft@123"
print(S4.isupper())

S5="INSPIREWEBSOFT"
print(S5.isupper())
Output :
 
False
False
False
False
True

replace() :

  • It replaces all the occurences of the old string with the new string.
Syntax :
 
<string>.replace(<old>,<new>)
Example :
 
S1="inspireWEBsoft"
print(S1.replace('i','@'))

S2="inspireWEBsoft inspirewebsoft"
print(S2.replace('WEB','###'))

S3="inspireWEBsoft"
print(S3.replace('Z','###'))
Output :
 
@nsp@reWEBsoft
inspire###soft inspirewebsoft
inspireWEBsoft    

split() :

  • It breaks up a string at the specified separators.
  • It returns a list of substring.
Syntax :
 
<string>.split( <character>)
<string>.split( <character>,<maxsplit>)
Example 1 :
 
S1="inspire web soft"
print(S1.split())              # by default split through space

S2="inspire web soft"
print(S2.split(' '))           # splitting through space

S3="inspirewebsoft"
print(S3.split('i'))

S4="inspirewebsoft"
print(S4.split('z'))
Output :
 
['inspire', 'web', 'soft']
['inspire', 'web', 'soft']
['', 'nsp', 'rewebsoft']
['inspirewebsoft']
Example 2 :
 
S1="inspirewebsoft"
print(S1.split(""))
Output :
 
print(S1.split(""))
ValueError: empty separator
Example 3 :
 
S1="inspire:web:soft"
print(S1.split(':',1))

S2="inspire:web:soft"
print(S2.split(':',0))

S3="inspire:web:soft"
print(S3.split(':',5))
Output :
 
['inspire', 'web:soft']
['inspire:web:soft']
['inspire', 'web', 'soft']
Note:- Maxsplit is optional and default value is 0. If an integer value ‘n’ is given for the second argument, the string is split into ‘n+1’ strings.

partition(separator) :

  • It is used to split the given string using the specified separator.
  • It returns a tuple with three parts : (substring_before_separator, separator_itself, substring_after_the_separator)
Syntax :
 
<string>.partition(separator)
Example 1 :
 
S1="inspirewebsoft.com@gmail.com"
print(S1.partition(" "))             #space

S2="inspirewebsoft.com@gmail.com"
print(S2.partition("@"))

S3="inspirewebsoft.com@gmail.com"
print(S3.partition("."))
Output :
 
('inspirewebsoft.com@gmail.com', '', '')
('inspirewebsoft.com', '@', 'gmail.com')
('inspirewebsoft', '.', 'com@gmail.com')
Example 2 :
 
S1="inspirewebsoft.com@gmail.com"
print(S1.partition(""))               #no space
Output :
 
print(S1.partition(""))                       
ValueError: empty separator

istitle() :

  • If string is titlecased , then its return True.
  • If the string is empty or not titlecased, then it returns False.
Syntax :
 
<string>.istitle()
Example :
 
S1="inspire web soft"
print(S1.istitle())

S2="Inspire Web Soft"
print(S2.istitle())

S3="Inspire 123Web Soft"
print(S3.istitle())

S4="Inspire Web123 Soft"
print(S4.istitle())

S5="Inspire web soft"
print(S5.istitle())
Output :
 
False
True
True
True
False

title() :

  • It converts the first character of each word to uppercase.
Syntax :
 
<string>.title()
Example :
 
S1="inspire web soft"
print(S1.title())

S2="Inspire Web Soft"
print(S2.title())

S3="Inspire 123Web Soft"
print(S3.title())

S4="Inspire Web123 Soft"
print(S4.title())

S5="Inspire web soft"
print(S5.title())
Output :
 
Inspire Web Soft
Inspire Web Soft
Inspire 123Web Soft
Inspire Web123 Soft
Inspire Web Soft

join(sequence) :

  • It returns a string in which the string elements have been joined by a string separator.
Syntax :
 
<string>.join(<sequence>)
Example :
 
S1="inspirewebsoft"
print("#".join(S1))

S2="inspire"
a='+'
print(a.join(S2))
Output :
 
i#n#s#p#i#r#e#w#e#b#s#o#f#t
i+n+s+p+i+r+e

swapcase() :

  • It converts and returns all uppercase characters into lowercase and vice-versa of the given string.
Syntax :
 
<string>.swapcase()
Example :
 
S1="inspireWEBsoft@123"
print(S1.swapcase())
Output :
 
INSPIREwebSOFT@123

find() :

  • It returns the lowest index in the string where the substring sub is found within the slice range of start and end.
  • It returns -1, if substring is not found.
Syntax :
 
<string>.find(<substring>)
<string>.find(<substring>, <start_index>, <end_index>)
Example :
 
S1="inspirewebsoft inspirewebsoft"
print(S1.find("web"))
print(S1.find("web",10, 28))
print(S1.find("web", 8, 15))
Output :
 
7
22
-1

strip() :

  • It removes leading (beginning of the string) and trailing (at the end of the string) whitespaces, if the parameter is not passed.
  • It removes the leading and trailing set of characters, if passed as a parameter.
Syntax :
 
<string>.strip()
<string>.strip(<characters>)
Example :
 
S1="    inspire    web    soft      "
print(S1.strip())

S2="@#%*inspire    web    soft%*%@"
print(S2.strip("@%#"))
Output :
 
inspire    web    soft
*inspire    web    soft%*
Note:- All possible substrings of given argument ‘@%#’ are matched with left and right of the S2 and if found, removed. That is, ‘@%#’, ‘@#’, ‘@%’, ‘%#’, ‘@’, ‘#’, ‘%’ and their reversed strings are matched, if any of these are found, it is removed from the left and right direction of the string, otherwise it will return the same string.

lstrip() :

  • It removes leading (beginning of the string) whitespaces, if the parameter is not passed.
  • It removes the leading set of characters, if passed as a parameter.
Syntax :
 
<string>.lstrip()
<string>.lstrip(<characters>)
Example :
 
S1="    inspire    web    soft      "
print(S1.lstrip())

S2="@#%*inspire    web    soft%*%@"
print(S2.lstrip("@%#"))
Output :
 
inspire    web    soft      
*inspire    web    soft%*%@
Note:- All possible substrings of given argument ‘@%#’ are matched with the left of the S2 and if found, removed. That is, ‘@%#’, ‘@#’, ‘@%’, ‘%#’, ‘@’, ‘#’, ‘%’ and their reversed strings are matched, if any of these are found, it is removed from the left direction of the string, otherwise it will return the same string.

rstrip() :

  • It removes trailing (at the end of the string) whitespaces, if the parameter is not passed.
  • It removes the trailing set of characters, if passed as a parameter.
Syntax :
 
<string>.rstrip()
<string>.rstrip(<characters>)
Example :
 
S1="    inspire    web    soft      "
print(S1.rstrip())

S2="@#%*inspire    web    soft%*%@"
print(S2.rstrip("@%#"))
Output :
 
    inspire    web    soft
@#%*inspire    web    soft%*
Note:- All possible substrings of given argument ‘@%#’ are matched with the right of the S2 and if found, removed. That is, ‘@%#’, ‘@#’, ‘@%’, ‘%#’, ‘@’, ‘#’, ‘%’ and their reversed strings are matched, if any of these are found, it is removed from the right direction of the string, otherwise it will return the same string.

startswith() :

  • It returns True, if the string starts with the specified value Otherwise False.
Syntax :
 
<string>.startswith(<value>)
<string>.startswith(<value>, <start_index>)
<string>.startswith(<value>, <start_index>, <end_index>)
Example :
 
S1="inspirewebsoft"
print(S1.startswith("ins"))

S2="inspirewebsoft"
print(S2.startswith("soft"))

S3="inspirewebsoft"
print(S3.startswith("ins", 4))

S4="inspinsrewebsoft"
print(S4.startswith("ins", 4, 15))
Output :
 
True
False
False
True

endswith() :

  • It returns True, if the string starts with the specified value Otherwise False.
Syntax :
 
<string>.endswith(<value>)
<string>.endswith(<value>, <start_index>)
<string>.endswith(<value>, <start_index>, <end_index>)
Example :
 
S1="inspirewebsoft"
print(S1.endswith("soft"))

S2="inspirewebsoft"
print(S2.endswith("tfos"))

S3="inspirewebsoft"
print(S3.endswith("soft", 4))

S4="inspinsrewebsoft"
print(S4.endswith("soft", 4, 15))    #last index not included
Output :
 
True
False
True
False

ord() :

  • It returns the ordinal value of the character passed as a parameter.
Syntax :
 
ord(<character>)
Example :
 
x=ord('A')
print(x)
Output :
 
65

chr() :

  • It returns the character of the ordinal value passed as a parameter.
Syntax :
 
chr(<ordinal_value>)    
Example :
 
x=chr(65)
print(x)    
Output :
 
A