Python String Methods | Set to 1 (find, find, start at, end, islower, isupper, bottom, top, swapcase & amp; title)

1. find (“string”, beg, end) : — this function is used to find the position of a substring in a string. It takes 3 arguments: substring, starting index (default 0) and ending index (default string length) .

  • Returns “-1” if string is not found in the given range.
  • Returns the first occurrence of a string if found.

2. rfind (“string”, beg, end) : — This function works like find (), but it returns the position of the last occurrence of the string.

# Python code to demonstrate how it works
# find () and rfind ()

str = " pythonengineering is for geeks "

str2 = "geeks"

 
# using find () to find the first occurrence of str2
# returns 8

print ( "The first occurrence of str2 is at:" , end = " ")

< p> print ( str . find (str2, 4 ))

  
# using rfind () to find the 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  

3. startwith ("string", beg, end) : — The purpose of this function is — return true if the function starts with the specified string (prefix), otherwise return false.

4.ndswith ("string", beg, end) : — The purpose of this function is — return true if the function ends with the specified string (suffix), otherwise return false.

# Python code to demonstrate how it works
# starts with () and ends with ()

str = "geeks"

str1 = "pythonengineeringportal"

 
# using startwith () to find if str
# starts with str1

if str1.startswith ( str ) :

print ( "str1 begins with:" + str )

else : print ( "str1 does not begin with:" + str )

 
# use the endwith () function 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 

5. islower ("string") : — This function returns true if all letters in the string are lowercase, otherwise — false.

6. isupper ("string") : — this function returns true if all letters in the string are uppercase , otherwise — false.

# Python code to demonstrate how it works
# isupper () and islower ( )

str = "GeeksforGeeks"

str1 = "geeks"

 
# check if all characters in str are uppercase

if str . isupper ():

print ( " All characters in str are upper cased " )

else : print ( "All characters in str are not upper cased" )

  
# check if all characters in str1 are in bottom case

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 

7. lower () : — This function returns a new string with all letters converted to lowercase .

8. upper () : — This function returns a new string with all letters uppercase .

9. swapcase () : — this function is used to replace string registers i.e. uppercase converted to lowercase and vice versa.

10. title () : — This function converts the string to heading case, i.e. the first letter of each word in the string is written in uppercase, and all the rest — in the bottom.

# Python code to demonstrate how it works
# upper (), lower (), swapcase () and title ()

str = "GeeksForGeeks is fOr GeeKs"

 
# Closing line lowercase

str1 = str . lower (); 

print ( "The lower case converted string is: " + str1)

  
# Uppercase closing line

str2 = str . upper (); 

print ( "The upper case converted string is: " + str2)

  
# Line coverage in its changed corpus

str3 = str . swapcase (); 

print ( "The swap case converted string is: " + str3)

  
# Caps closing line

str4 = str . title (); 

print ( "The title case converted string is: " + str4)

Output:

 The lower case converted string is: pythonengineering 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 

This article is contributing to Manjeet Singh If you love Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or post the article [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.

Please write in comments if you find anything wrong or if you`d like to share more information on the topic discussed above.