Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)
Last Updated :
16 Jun, 2022
Some of the string basics have been covered in the below articles Strings Part-1 Strings Part-2 The important string methods will be discussed in this article1. find(“string”, beg, end) :- This function is used to find the position of the substring within a string.It takes 3 arguments, substring , starting index( by default 0) and ending index( by default string length) .
It returns “-1 ” if string is not found in given range.
It returns first occurrence of string if found.
2. rfind(“string”, beg, end) :- This function has the similar working as find(), but it returns the position of the last occurrence of string.
Python
str = "geeksforgeeks is for geeks"
str2 = "geeks"
print ("The first occurrence of str2 is at : ", end="")
print (str.find( str2, 4) )
print ("The last occurrence of str2 is at : ", end="")
print ( str.rfind( str2, 4) )
Output:
The first occurrence of str2 is at : 8
The last occurrence of str2 is at : 21
Time complexity : O(n) Auxiliary Space : O(1)
3. startswith(“string”, beg, end) :- The purpose of this function is to return true if the function begins with mentioned string(prefix) else return false.4. endswith(“string”, beg, end) :- The purpose of this function is to return true if the function ends with mentioned string(suffix) else return false.
Python3
str = "geeks"
str1 = "geeksforgeeksportal"
if str1.startswith(str):
print ("str1 begins with : " + str)
else : print ("str1 does not begin with : "+ str)
if str1.endswith(str):
print ("str1 ends with : " + str)
else :
print ("str1 does not end with : " + str)
Output:
str1 begins with : geeks
str1 does not end with : geeks
Time complexity : O(n) Auxiliary Space : O(1)
5. islower(“string”) :- This function returns true if all the letters in the string are lower cased, otherwise false.6. isupper(“string”) :- This function returns true if all the letters in the string are upper cased , otherwise false.
Python3
str = "GeeksforGeeks"
str1 = "geeks"
if str.isupper() :
print ("All characters in str are upper cased")
else :
print ("All characters in str are not upper cased")
if str1.islower() :
print ("All characters in str1 are lower cased")
else :
print ("All characters in str1 are not lower cased")
Output:
All characters in str are not upper cased
All characters in str1 are lower cased
Time complexity : O(n) Auxiliary Space : O(1)
7. lower() :- This function returns the new string with all the letters converted into its lower case .8. upper() :- This function returns the new string with all the letters converted into its upper case .9. swapcase() :- This function is used to swap the cases of string i.e upper case is converted to lower case and vice versa.10. title() :- This function converts the string to its title case i.e the first letter of every word of string is upper cased and else all are lower cased.
Python3
str = "GeeksForGeeks is fOr GeeKs"
str1 = str.lower();
print (" The lower case converted string is : " + str1)
str2 = str.upper();
print (" The upper case converted string is : " + str2)
str3 = str.swapcase();
print (" The swap case converted string is : " + str3)
str4 = str.title();
print (" The title case converted string is : " + str4)
Output:
The lower case converted string is : geeksforgeeks is for geeks
The upper case converted string is : GEEKSFORGEEKS IS FOR GEEKS
The swap case converted string is : gEEKSfORgEEKS IS FoR gEEkS
The title case converted string is : Geeksforgeeks Is For Geeks
Time complexity : O(n) Auxiliary Space : O(1)
VIDEO