Python String Methods - Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)
Last Updated :
23 Jul, 2025
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 three arguments:
- substring: The string to search for.
- beg (starting index, default = 0).
- end (ending index, default = string length).
returns -1 if the substring is not found in the given range.
returns the first occurrence of the substring 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"
s = "geeks"
# using find() to find first occurrence of s
print ("The first occurrence of s is at : ", end="")
print (str.find( s, 4) )
# using rfind() to find last occurrence of s
print ("The last occurrence of s is at : ", end="")
print ( str.rfind( s, 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.
Python
str = "geeks"
s = "geeksforgeeksportal"
# using startswith() to find if str
# starts with s
if s.startswith(str):
print ("s begins with : " + str)
else : print ("s does not begin with : "+ str)
# using endswith() to find
# if str ends with s
if s.endswith(str):
print ("s ends with : " + str)
else :
print ("s 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.
Python
# 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.
Python
str = "GeeksForGeeks is fOr GeeKs"
# Converting string into its lower case
s1 = str.lower();
print (" lower case string : " + str1)
# Converting string into its upper case
s2 = str.upper();
print (" upper case string : " + str2)
# Converting string into its swapped case
s3 = str.swapcase();
print (" swap case string : " + str3)
# Converting string into its title case
s4 = str.title();
print (" title case string : " + 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
isupper(), islower(), lower(), upper() in Python and their applications Python provides several built-in string methods to work with the case of characters. Among them, isupper(), islower(), upper() and lower() are commonly used for checking or converting character cases. In this article we will learn and explore these methods:1. isupper() MethodThe isupper() method che
2 min read
Regex in Python to put spaces between words starting with capital letters Given an array of characters, which is basically a sentence. However, there is no space between different words and the first letter of every word is in uppercase. You need to print this sentence after the following amendments: Put a single space between these words. Convert the uppercase letters to
2 min read
String lower() Method in Python lower() method in Python converts all uppercase letters in a string to their lowercase. This method does not alter non-letter characters (e.g., numbers, punctuation). Let's look at an example of lower() method:Pythons = "HELLO, WORLD!" # Change all uppercase letters to lowercase res = s.lower() prin
3 min read
Capitalize Each String in a List of Strings in Python In Python, manipulating strings is a common task, and capitalizing each string in a list is a straightforward yet essential operation. This article explores some simple and commonly used methods to achieve this goal. Each method has its advantages and use cases, providing flexibility for different s
3 min read
Python String islower() Method The islower() method in Python checks if all characters in a string are lowercase. It returns True if all alphabetic characters are lowercase, otherwise, it returns False, if there is at least one uppercase letter.Let's look at a quick example of using the islower() method.Pythons = "hello" res = s.
2 min read
Python String islower() Method The islower() method in Python checks if all characters in a string are lowercase. It returns True if all alphabetic characters are lowercase, otherwise, it returns False, if there is at least one uppercase letter.Let's look at a quick example of using the islower() method.Pythons = "hello" res = s.
2 min read