Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)

07 Jul, 2022
Some of the string methods are covered in the set 3 belowString Methods Part- 1
More methods are discussed in this article
1. len() :- This function returns the length of the string.
2. count(“string”, beg, end) :- This function counts the occurrence of mentioned substring in whole string. This function takes 3 arguments, substring, beginning position( by default 0) and end position(by default string length).
# Python code to demonstrate working of # len() and count()str = "geeksforgeeks is for geeks"   # Printing length of string using len()print (" The length of string is : ", len(str));  # Printing occurrence of "geeks" in string# Prints 2 as it only checks till 15th elementprint (" Number of appearance of ""geeks"" is : ",end="")print (str.count("geeks",0,15))
Output:
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
 The length of string is :  26
 Number of appearance of geeks is : 2

3. center() :- This function is used to surround the string with a character repeated both sides of string multiple times. By default the character is a space. Takes 2 arguments, length of string and the character.
4. ljust() :- This function returns the original string shifted to left that has a character at its right. It left adjusts the string. By default the character is space. It also takes two arguments, length of string and the character.5. rjust() :- This function returns the original string shifted to right that has a character at its left. It right adjusts the string. By default the character is space. It also takes two arguments, length of string and the character.
# Python code to demonstrate working of # center(), ljust() and rjust()str = "geeksforgeeks"   # Printing the string after centering with '-'print ("The string after centering with '-' is : ",end="")print ( str.center(20,'-'))  # Printing the string after ljust()print ("The string after ljust is : ",end="")print ( str.ljust(20,'-'))  # Printing the string after rjust()print ("The string after rjust is : ",end="")print ( str.rjust(20,'-'))
Output:
The string after centering with '-' is : ---geeksforgeeks----
The string after ljust is : geeksforgeeks-------
The string after rjust is : -------geeksforgeeks

6. isalpha() :- This function returns true when all the characters in the string are alphabets else returns false.
7. isalnum() :- This function returns true when all the characters in the string are combination of numbers and/or alphabets else returns false.
8. isspace() :- This function returns true when all the characters in the string are spaces else returns false.
# Python code to demonstrate working of # isalpha(), isalnum(), isspace()str = "geeksforgeeks"str1 = "123"   # Checking if str has all alphabets if (str.isalpha()):       print ("All characters are alphabets in str")else : print ("All characters are not alphabets in str")  # Checking if str1 has all numbersif (str1.isalnum()):       print ("All characters are numbers in str1")else : print ("All characters are not numbers in str1")  # Checking if str1 has all spacesif (str1.isspace()):       print ("All characters are spaces in str1")else : print ("All characters are not spaces in str1")
Output:
All characters are alphabets in str
All characters are numbers in str1
All characters are not spaces in str1

9. join() :- This function is used to join a sequence of strings mentioned in its arguments with the string.
# Python code to demonstrate working of # join()str = "_"str1 = ( "geeks", "for", "geeks" )  # using join() to join sequence str1 with strprint ("The string after joining is : ", end="")print ( str.join(str1))
Output:
The string after joining is : geeks_for_geeks

Python
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java