Python Strings inbuilt Methods with examples

Python String

Today we are going to learn about Python Strings inbuilt Methods with examples and how to use them.

These are string Strings inbuilt Methods with examples, we have mentioned each method with description and program example.


Method

Description

Example


1.title()

This function is used to convert a string to a Title format. Title format means it converts the first character of the string to uppercase and keeps all other letters to lower case.

strvar = "sachin is sachin tendulkar"
strvar = strvar.title();
print(strvar)

Output

Sachin Is Sachin Tendulkar

2. lower()

This function is used to convert a string to all lower case strings.

strvar = "Sachin is Sachin Tendulkar"
strvar = strvar.lower();
print(strvar)

Output

sachin is sachin tendulkar

3. upper()

This function is used to convert a string to all upper case strings.

strvar = "Sachin is Sachin Tendulkar"
strvar = strvar.upper();
print(strvar)

Output

SACHIN IS SACHIN TENDULKAR


4. islower(“string”)

This function is used to verify all the letters of the string are in lower case or not. If the string is all lower case this function returns true.
If the string is not all lower case this function returns false.

strvar = "Sachin is Sachin Tendulkar"
if strvar.islower() :
 print ("All characters in strvar are lower cased")
else :
 print ("All characters in strvar are not lower cased")

Output

sachin is sachin tendulkar


5.isupper(string)

This function is used to verify all the letters of the string are upper case or not. If the string is all upper case this function returns true. If the string is not all upper case this function returns false.

strvar = "SACHIN TENDULKAR"
if str.isupper(strvar) :
 print("All characters  are upper cased")
else : 
 print("All characters  are not upper cased")

Output

All characters  are upper cased


6. swapcase()

This function is used to convert a lower case string to all upper case strings or to convert an upper case string to all lower case strings.
The Operation of this function depends on the input. If the input is the lower case it converts to the upper, if the input is the upper case it converts to the lower case.

strvar = "Sachin is Sachin Tendulkar"
strvar = strvar.swapcase();
print(strvar)

Output

sACHIN IS sACHIN tENDULKAR

7.startswith(“string”, begin, end)

This function is used to check if a string begins with a substring.

Arguments :

“string” – This is the substring whose index is requested.
begin – starting index of search, if not given then 0 by default.
end – ending index of the search scope, if not given then string length.

Return values :

  • If it finds the substring at the beginning then it returns true.
  • If it does not find the substring at the beginning then it returns false.

strvar = "Sachin is Sachin Tendulkar"
strvar2 = "chin"
if strvar.startswith(strvar2):
 print ("strvar begins with : "+ strvar2)
else :
 print ("strvar does not begin with : "+ strvar2)

Output

strvar does not begin with : chin

8.endswith(“string”, beg, end)

This function is used to check if a string ends with a substring.

Arguments :

“string” – This is the substring whose index is requested.
begin – starting index of search, if not given then 0 by default.
end – ending index of the search scope, if not given then string length.

Return values :

  • If it finds the substring at the end then it returns true.
  • If it does not find the substring at the end then it returns false.

strvar = "Sachin is Sachin Tendulkar"
strvar2 = "chin"
if strvar.endswith(strvar2):
 print ("strvar ends with : " + strvar2)
else :
 print ("strvar does not ends with : "+ strvar2)

Output

strvar does not ends with : chin

9.find(“string”, begin, end)

This function finds the index of the first occurrence of a substring within a string.

Arguments :

“string” – This is the substring that index is requested.
begin – starting index of search, if not given then 0 by default.
end – ending index of search scope, if not given then string length.

Return values :

  • It returns “-1 ” if the string is not found.
  • It returns the index of the first occurrence of string if the string is found.

strvar = "Sachin is a Great Cricketer"
strvar2 = "chin"
print ('Index of found string:',strvar.find( strvar2))

Output

Index of found string: 2

10.rfind(“string”, begin, end)

This function finds the index of the last occurrence of a sub-string within a string.

Arguments :

“string” – This is the substring which index is requested.
begin – starting index of search, if not given then 0 by default.
end – ending index of search scope, if not given then string length.

Return values :

  • It returns “-1 ” if the string is not found.
  • It returns the index of the first occurrence of string if the string is found.

strvar = "Sachin is Sachin Tendulkar"
strvar2 = "chin"
print ('index of found string:',strvar.find( strvar2, 1) )

Output

Index of found string: 2

11. len()

First in the line is len() function. This function is used to get the length of the string in python.

strvar = "Sachin"
length = len(strvar)
print (" The length of string is : ", length);

Output

The length of string is :  6


12. count(“string”, begin, end)

This function is used to count the number of times a substring is present in a string.

Arguments :

“string” – This is the substring whose index is requested.
begin – starting index of search, if not given then 0 by default.
end – ending index of the search scope, if not given then string length.

strvar = "Sachin is The Great Sachin Tendulkar"
print ('index of substring is :',strvar.count("Sachin",0,30))

Output

index of substring is : 2


13.center()

This function is used to align a string to the center of the number length passed as the first argument.

Arguments:

  • Length of resulting string you want.
  • The character which you want to fill on both sides of a centered string. Default is space.
strvar = "Sachin"
print ( strvar.center(20,'*'))

Output

Sachin

14.ljust()

This function is used to align a string to the left.

Arguments :

  • Length of resulting string you want.
  • The character which you want to fill on the right of left-aligned string. Default is space.
strvar = "Sachin"
print ( strvar.ljust(10,'*'))

Output

Sachin****

15.rjust()

This function is used to align a string to the Right.

Arguments :

  • Length of resulting string you want.
  • The character which you want to fill on left of the left-aligned string. Default is space.

strvar = "Sachin"
print ( strvar.rjust(10,'*'))

Output

 ****Sachin

16. isalpha()

This function is used to verify if all characters in a string are alphabets or not.

  • If all characters are alphabets then it returns true.
  • If all characters are not alphabets then it returns false.
strvar = "Sachin"
if (strvar.isalpha()):
print ("All characters are alphabets in strvar")
else :
print ("All characters are not alphabets in strvar")

Output

All characters are alphabets in strvar

17.isalnum()

This function is used to verify if the string is alphanumeric or not.

  • If the string is alphanumeric then it returns true.
  • If the string is not alphanumeric alphabets then it returns false.
  • alphanumeric means combination of numbers and characters.

strvar = "Sachin123"
if (strvar.isalnum()):
 print ("All characters are numbers in strvar")
else :
 print ("All characters are not numbers in strvar")

Output

All characters are not numbers in strvar

18.isspace()

This function is used to check if all characters in a string are white spaces.

  • If the string has all-white spaces it returns true.
  • If the string does not have all white spaces it returns false.

strvar = "Sachin"
if (strvar.isspace()):
 print ("All spaces in strvar")
else :
 print ("All characters are not spaces in strvar")

Output

All characters are not spaces in strvar

19. join()

Strings in Python have the join() method. This method takes all items in an iterable form and joins them into one string with the help of a separator.
The calling string must be specified as the separator.

syntax

string.join(iterable)

iterable: Any iterable object where all the returned values are strings

strsep = "_"
str = ( "Sachin", "is", "Great" )
print ("The string after applying the join method is : ", end="")
print ( strsep .join(str))

Output

The string after applying the join method is : Sachin_is_Great

20. strip()

This function is used to remove all the leading and trailing occurrences of a character that is passed as an argument

strvar = "****Sachin****"
print ( str.strip('*') )

Output

 Sachin

21.lstrip()

This function is used to remove all the trailing occurrences of a character that is passed as an argument.

strvar = "Sachin***" 
print ( strvar.lstrip('') )

Output

Sachin

22.rstrip()

This function is used to remove all the leading occurrences of a character that is passed as an argument.

strvar = "***Sachin" 
print ( strvar.rstrip('') )

Output

 Sachin

23.lstrip()

This function is used to remove all the trailing occurrences of a character that is passed as an argument.

strvar = "Sachin***" 
print ( strvar.lstrip('') )

Output

 Sachin

24. min(“string”)

This function checks each alphabet in the string and returns the least value character on a scale of 1-26 ( a to z).

strvar = "Sachin"
print (min(strvar))

Output

a

25.max(“string”)

This function checks each alphabet in the string and returns the highest value character on a scale of 1-26 ( a to z).

strvar = "Sachin"
print (max(strvar))

Output

S

26. maketrans() and translate()

This will make a character mapping of each character of strvar1 with strvar2 characters. Now if we call the translate() method on strvar string, it will replace the mapped characters which we created using maketrans().

some chracters got replace like S-> D , a->r ,c->a,h->v,i->i,n->d

strvar = "Sachin is Great!"
strvar1 = "Sachin"
strvar2 = "Dravid"
mapping = maketrans( strvar1, strvar2 )
print(strvar.translate(mapping))

Output

Dravid id Grert

27.replace()

This is used to replace an existing substring with a new substring in a string

Arguments:

  • old substring which needs replacement
  • new substring to replace an old substring
  • A number of occurrences you want to replace. Default is all.
strvar = "Sachin is The Great Sachin Tendulkar"
strvar1 = "Sachin"
strvar2 = "Dravid"
print (strvar.replace( strvar1, strvar2, 2))

Output

Dravid is the The Great Dravid Tendulkar"

27. expandtabs()

This function is used to expand the ‘\t’ tab escape sequence to white spaces.

Syntax:

string.tabsize(tabsize)

Parameters:

A number of white spaces characters are to be replaced for one tab character. Default is 8.

strvar = 'Sachin\tis\tGreat'
print (strvar.expandtabs())

Output

Sachin is Great