Open In App

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

Last Updated : 07 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Some of the string methods are covered in the set 3 below
String 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 element
print (" Number of appearance of ""geeks"" is : ",end="")
print (str.count("geeks",0,15))


Output:

 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 numbers
if (str1.isalnum()):
       print ("All characters are numbers in str1")
else : print ("All characters are not numbers in str1")
  
# Checking if str1 has all spaces
if (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 str
print ("The string after joining is : ", end="")
print ( str.join(str1))


Output:

The string after joining is : geeks_for_geeks


Similar Reads

Python String | ljust(), rjust(), center()
String alignment is frequently used in many day-day applications. Python in its language offers several functions that helps to align string. Also, offers a way to add user specified padding instead of blank space. These functions are : str.ljust(s, width[, fillchar]) str.rjust(s, width[, fillchar]) str.center(s, width[, fillchar]) These functions
3 min read
String rjust() and ljust() in python()
1. String rjust() The string rjust() method returns a new string of given length after substituting a given character in left side of original string. Syntax: string.rjust(length, fillchar) Parameters: length: length of the modified string. If length is less than or equal to the length of the original string then original string is returned. fillch
2 min read
Python | Pandas Series.str.ljust() and rjust()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas .ljust() and .rjust() are Text methods used to deal with text data in series. Since these methods are applicable only on strings,
3 min read
List Methods in Python | Set 1 (in, not in, len(), min(), max()...)
List methods are discussed in this article. 1. len() :- This function returns the length of list. List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(len(List)) Output: 10 2. min() :- This function returns the minimum element of list. List = [2.3, 4.445, 3, 5.33, 1.054, 2.5] print(min(List)) Output: 1.054 3. max() :- This function returns the maximum eleme
2 min read
Python String rjust() Method
Python String rjust() method returns a new string of a given length after substituting a given character on the left side of the original string. Python String rjust() Method Syntax Syntax: string.rjust(length, fillchar) Parameters: length: length of the modified string. If length is less than or equal to the length of the original string then orig
2 min read
Python String ljust() Method
Python String ljust() method left aligns the string according to the width specified and fills the remaining space of the line with blank space if ‘fillchr‘ argument is not passed. Python String ljust() Method Syntax: Syntax: ljust(len, fillchr) Parameters: len: The width of string to expand it.fillchr (optional): The character to fill in the remai
2 min read
Python String isalnum() Method
Python String isalnum() method checks whether all the characters in a given string are either alphabet or numeric (alphanumeric) characters. Python String isalnum() Method Syntax: Syntax: string_name.isalnum() Parameter: isalnum() method takes no parameters Return: True: If all the characters are alphanumeric False: If one or more characters are no
1 min read
numpy string operations | rjust() function
numpy.core.defchararray.rjust(arr, width, fillchar=' ') is another function for doing string operations in numpy. It returns an array with the elements of arr right-justified in a string of length width.It fills remaining space of each array element using fillchr parameter.If fillchr is not passed then it fills remaining spaces with blank space. Pa
2 min read
numpy string operations | ljust() function
numpy.core.defchararray.ljust(arr, width, fillchar=' ') is another function for doing string operations in numpy. It returns an array with the elements of arr left-justified in a string of length width. It fills remaining space of each array element using fillchr parameter. If fillchr is not passed then it fills remaining spaces with blank space. P
2 min read
Python String isspace() Method
Python String isspace() method returns “True” if all characters in the string are whitespace characters, Otherwise, It returns “False”. This function is used to check if the argument contains all whitespace characters, such as: ‘ ‘ – Space‘\t’ – Horizontal tab‘\n’ – Newline‘\v’ – Vertical tab‘\f’ – Feed‘\r’ – Carriage returnPython String isspace()
2 min read
Article Tags :
Practice Tags :