Python String Methods – Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)
Last Updated : 02 Apr, 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 ch
2 min read
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 th
4 min read
Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
Some of the string methods are covered in the below sets.String Methods Part- 1 String Methods Part- 2More methods are discussed in this article1. strip():- This method is used to delete all the leading and trailing characters mentioned in its argument.2. lstrip():- This method is used to delete all
4 min read
Find the first repeated word in a string in Python using Dictionary
We are given a string that may contain repeated words and the task is to find the first word that appears more than once. For example, in the string "Learn code learn fast", the word "learn" is the first repeated word. Let's understand different approaches to solve this problem using a dictionary. U
3 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
Python - Ways to sort list of strings in case-insensitive manner
Sorting strings in a case-insensitive manner ensures that uppercase and lowercase letters are treated equally during comparison. To do this we can use multiple methods like sorted(), str.casefold(), str.lower() and many more. For example we are given a list of string s = ["banana", "Apple", "cherry"
2 min read
Conversion of whole String to uppercase or lowercase using STL in C++
Given a string, convert the whole string to uppercase or lowercase using STL in C++. Examples: For uppercase conversionInput: s = "String"Output: s = "STRING" For lowercase conversionInput: s = "String"Output: s = "string" Functions used : transform : Performs a transformation on given array/string.
1 min read
Is Python Case-Sensitive? Yes, Here's What You Need to Know
Yes, Python differentiates between uppercase and lowercase characters, making it a case-sensitive programming language. This distinction means that Python treats identifiers such as variable, Variable, and VARIABLE as entirely separate entities. Understanding this case sensitivity is crucial for any
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: [GFGTABS] Python s = "HELLO, WORLD!" # Change all uppercase letters to lowercas
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