In this post, we are going to learn the Python strings inbuilt function part 2. These functions are very useful when we do operations on strings in our programs. By using these built-in functions we can get a lot of information about the string, we can manipulate the string, change the alignment, check the content of the string, and much more is available in string built-in functions that help programmers a lot.
We will be covering all the strings inbuilt functions. We will try to break it into multiple posts so we can explain each function with its usage for your better understanding. So let us begin with function with code example
1. len()
First in the line is len() function. This function is used to get the length of string in python. As we can see from the simple code below we are trying to get the length of the string.
strvar = "Sachin"
length = len(strvar)
print (" The length of string is : ", length);
Output
The length of string is : 6
Read More :String inbuilt functions
2.count(“string”, begin, end)
This function is used to count the number of times a substring 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
3.center()
This function is used to align a string to the center of the number length passed as the first argument.
This function accepts two 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
4. ljust()
This function is used to align a string to the left.
This function accepts two 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****
5.rjust()
This function is used to align a string to the Right.
This function accepts two 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
6. 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
7.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
8.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
9. 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.
The syntax is:
string.join(iterable)
iterable: Any iterable object where all the returned values are strings
let us understand this with the help of example:
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