String Functions And Methods In Python Programing - Coderz*United

String Functions And Methods In Python Programing






Functions which perform manipulation on strings. 

string.capitalize() 

Returns copy of the string with the first character capitalized. 


Examples of string.capitalize() in interactive mode: 

'ahoy123'.capitalize() 

Out[1]: 'Ahoy123' 


'wow'.capitalize() 

Out[2]: 'Wow' 


''.capitalize() 

Out[3]: '' 


' '.capitalize() 

Out[4]: ' ' 


'..'.capitalize() 

Out[5]: '..'

string.isalnum() 

Returns True if the characters in the string are alphanumeric (alphabets or numbers), otherwise False.


Examples of string.isalnum() in interactive mode: 

"12345".isalnum() 

Out[1]: True 


"abc".isalnum() 

Out[2]: True 


"abc123".isalnum() 

Out[3]: True 


" ".isalnum() 

Out[4]: False 


"//".isalnum() 

Out[5]: False 


''.isalnum() 

Out[6]: False 

string.isalpha() 

Returns True if all the characters in the string are alphabetic, otherwise False.


Examples of string.isalpha() in interactive mode: 

'abc'.isalpha() 

Out[1]: True 


'abc123'.isalpha() 

Out[2]: False 


'//'.isalpha() 

Out[3]: False 


''.isalpha() 

Out[4]: False 

string.isdigit() 

Returns True if all the characters in the string are digits, otherwise False.


Examples of string.isdigit() in interactive mode:  

'1234'.isdigit() 

Out[1]: True 


'abc'.isdigit() 

Out[2]: False 


'abc123'.isdigit() 

Out[3]: False 


''.isdigit() 

Out[4]: False 


' 1234 '.isdigit() 

Out[5]: False 

string.islower() 

Returns True if all the cased characters in the string are lowercase, otherwise False.


Examples of string.islower() in interactive mode:

'abc'.islower() 

Out[1]: True 


'abc123'.islower() 

Out[2]: True 


'123a34/+'.islower() 

Out[3]: True 


'Abc'.islower() 

Out[4]: False 


''.islower() 

Out[5]: False 

string.isspace() 

Returns True if there are only whitespace characters in the string, otherwise False. 


Examples of string.isspace() in interactive mode: 

' '.isspace() 

Out[1]: True 


''.isspace() 

Out[2]: False 


'abc def'.isspace() 

Out[3]: False 

string.isupper() 

Returns True if all the cased characters in the string are uppercase, otherwise False. 


Examples of string.isupper() in interactive mode: 

'ABC'.isupper() 

Out[1]: True 


'ABC123'.isupper() 

Out[2]: True 


'ABC1D'.isupper() 

Out[3]: True 


'ABC1d'.isupper() 

Out[4]: False 

string.lower() 

Returns a copy of the string with all the cased characters converted to lowercase.


Examples of string.lower() in interactive mode: 

‘ABC’.lower() 

Out[1]: 'abc' 


'ABC123'.lower()

Out[2]: 'abc123' 


'ABC1d'.lower() 

Out[3]: 'abc1d' 


''.lower() 

Out[4]: '' 


' abCDEFG//#4'.lower() 

Out[5]: ' abcdefg//#4' 

string.upper() 

Returns a copy of the string with all the cased characters converted to uppercase. 


Examples of string.upper() in interactive mode: 

'abc'.upper() 

Out[51]: 'ABC' 


'abcDEghi'.upper() 

Out[52]: 'ABCDEGHI' 


'1234'.upper() 

Out[53]: '1234' 


'abc123'.upper() 

Out[55]: 'ABC123' 


'123 + 369'.upper() 

Out[56]: '123 + 369' 

string.find(sub[,start[,end]]) 

Returns the lowest index of the string where the substring sub is found within the slice range of start and end. Returns -1 if sub is not found. 


Examples of string.find(sub[,start[,end]]) in interactive mode:

'amazing'.find('in') 

Out[57]: 4 


'amazing'.find('a') 

Out[58]:


'hilarious'.find('i') 

Out[59]:


'amazing'.find('a',1,4) 

Out[61]:


'hilarious'.find('io',2,6) 

Out[65]: -1 


'hilarious'.find('io',2,8)

Out[66]:


'hilarious'.find('io',2) 

Out[67]: 5