Open In App

Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)

Last Updated : 16 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 article
1. 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




# Python code to demonstrate working of
# find() and rfind()
str = "geeksforgeeks is for geeks"
str2 = "geeks"
 
# using find() to find first occurrence of str2
# returns 8
print ("The first occurrence of str2 is at : ", end="")
print (str.find( str2, 4) )
 
# using rfind() to find last occurrence of str2
# returns 21
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




# Python code to demonstrate working of
# startswith() and endswith()
str = "geeks"
str1 = "geeksforgeeksportal"
 
# using startswith() to find if str
# starts with str1
if str1.startswith(str):
        print ("str1 begins with : " + str)
else : print ("str1 does not begin with : "+ str)
 
# using endswith() to find
# if str ends with str1
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




# Python code to demonstrate working of
# isupper() and islower()
str = "GeeksforGeeks"
str1 = "geeks"
 
# checking if all characters in str are upper cased
if str.isupper() :
    print ("All characters in str are upper cased")
else :
    print ("All characters in str are not upper cased")
 
# checking if all characters in str1 are lower 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




# Python code to demonstrate working of
# upper(), lower(), swapcase() and title()
str = "GeeksForGeeks is fOr GeeKs"
 
# Converting string into its lower case
str1 = str.lower();
print (" The lower case converted string is : " + str1)
 
# Converting string into its upper case
str2 = str.upper();
print (" The upper case converted string is : " + str2)
 
# Converting string into its swapped case
str3 = str.swapcase();
print (" The swap case converted string is : " + str3)
 
# Converting string into its title case
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)



Similar Reads

Python | Pandas Series.str.lower(), upper() and title()
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. Python has some inbuilt methods to convert a string into a lower, upper, or Camel case. But these methods don't work on lists and other multi-st
4 min read
Python string swapcase() Method
Python String swapcase() method converts all uppercase characters to lowercase and vice versa of the given string and returns it. Syntax: string_name.swapcase() Parameter: The swapcase() method does not take any parameter. Return Value: The swapcase() method returns a string with all the cases changed. Example Below is the Python implementation of
1 min read
Python String rfind() Method
Python String rfind() method returns the rightmost index of the substring if found in the given string. If not found then it returns -1. Python String rfind() Method Syntax Syntax: str.rfind(sub, start, end) Parameters: sub: It’s the substring that needs to be searched in the given string. start: Starting position where the sub needs to be checked
3 min read
numpy string operations | swapcase() function
numpy.core.defchararray.swapcase(arr) function return element-wise a copy of the string with uppercase characters converted to lowercase and lowercase characters converted to uppercase. Syntax: numpy.char.swapcase(arr) Parameters: arr : [ array_like ] Input array which may be str or unicode. Returns : [ndarray] Output lowercased array of str or uni
1 min read
Python regex to find sequences of one upper case letter followed by lower case letters
Write a Python Program to find sequences of one upper case letter followed by lower case letters. If found, print 'Yes', otherwise 'No'. Examples: Input : GeeksOutput : YesInput : geeksforgeeksOutput : NoPython regex to find sequences of one upper case letter followed by lower case lettersUsing re.search() To check if the sequence of one upper case
2 min read
Python | Pandas Series.str.swapcase()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas provide a method to swap case of each string in a series. This means lower case characters will be converted into uppercase and U
3 min read
numpy string operations | rfind() function
numpy.core.defchararray.find(arr, sub, start=0, end=None) is another function for doing string operations in numpy.It returns the highest index in the string where substring sub is found for each element in arr.It returns -1 if sub is not contained within [start, end]. Parameters: arr : array_like of str or unicode. sub : [str or unicode] The subst
2 min read
Python | Pandas Series.str.rfind()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.Pandas str.rfind() method is used to search a substring in each string present in a series from the Right side. If the string is found, i
3 min read
Python program to count upper and lower case characters without using inbuilt functions
Given a string that contains both upper and lower case characters in it. The task is to count a number of upper and lower case characters in it. Examples :Input : Introduction to Python Output : Lower Case characters : 18 Upper case characters : 2 Input : Welcome to GeeksforGeeks Output : Lower Case characters : 19 Upper case characters: 3Method 1:
3 min read
Python String isupper() method
Python String isupper() method returns whether all characters in a string are uppercase or not. Python String isupper() method Syntax Syntax: string.isupper() Returns: True if all the letters in the string are in the upper case and False if even one of them is in the lower case. Python String isupper() method Examples C/C++ Code Output: TrueExample
2 min read
Article Tags :
Practice Tags :